Wstęp

Celem badania jest analiza porównawcza ryzyka rozumianego jako oszacowanie funkcji warunkowej wariancji w modelach klasy GARCH. W tym celu wybrano dwa modele z rodziny GARCH- …. Zbudowano portfel składający się z czterech kryptowalut, w którym każdego dnia udziały procentowe każdej z nich są proporcjonalne do ich kapitalizacji rynkowej oraz przeprowadzono analizę porównawczą oszacowań funkcji warunkowej wariancji w okresie in-sample oraz oszacowań wartości narażonej na ryzyko uzyskanych za pomocą rozpatrywanych modeli GARCH w okresie out-of-sample.

Dane modelu

Dane potrzebne do przeprowadzenia badania pochodzą z serwisu https://coinmarketcap.com/. Wybrano do analizy notowania kursów: tron, cosmos, neo, waves, nano. Próba pochodzi z okresu od stycznia do sierpnia.

Import i wstępna obróbka danych

library(lmtest)
library(xts)
library(RCurl)
library(rlist)
library(urca)
library(fBasics)
library(dygraphs)
library(tseries)
library(car)
library(rugarch)
library(fGarch)
library(timeSeries)
library.path <- .libPaths("/Library/Frameworks/R.framework/Versions/3.6/Resources/library")
scrapData <- function(x){
  
  # function to scrap the OHLC data from 
  # www.coinmarketcap.com
  
  # load packages
  library(XML)
  library(RCurl)
  library(rlist)
  
  # set locale to English
  Sys.setlocale("LC_TIME", "C")
  
  # set url
  theurl <- getURL(
    paste0("https://coinmarketcap.com/currencies/", 
           x, 
           "/historical-data/?start=20130428&end=21000101"),
    .opts = list(ssl.verifypeer = FALSE))
  
  # read html source
  tables <- readHTMLTable(theurl, stringsAsFactors = FALSE)
  
  # clean the list object
  tables <- list.clean(tables, fun = is.null, recursive = FALSE)
  
  # unlist the list
  table <- tables[[1]]
  
  # rename variables
  table$Open <- table$`Open*`
  table$`Open*` <- NULL
  table$Close <- table$`Close**`
  table$`Close*` <- NULL
  
  # convert characters to numericals
  table$Open      <- as.numeric(gsub(",", "", table$Open))
  table$High      <- as.numeric(gsub(",", "", table$High))
  table$Low       <- as.numeric(gsub(",", "", table$Low))
  table$Close     <- as.numeric(gsub(",", "", table$Close))
  table$Volume    <- as.numeric(gsub(",", "", table$Volume))
  table$MarketCap <- as.numeric(gsub(",", "", table[, "Market Cap"]))
  table[, "Market Cap"] <- NULL
  
  # convert the date from chr into Date
  table$Date <- as.Date(table$Date, format = "%b %d, %Y")
  
  # sort the data  
  table <- table[order(as.numeric(table$Date)), ]
  table <- table[, c("Date", "Open", "High", "Low", "Close", 
                     "Volume",  "MarketCap")]
  
  return(table)
}

####################################################################################################

# function call
library(tidyverse)
## Registered S3 methods overwritten by 'ggplot2':
##   method         from 
##   [.quosures     rlang
##   c.quosures     rlang
##   print.quosures rlang
## ── Attaching packages ─────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.1     ✔ purrr   0.3.2
## ✔ tibble  2.1.3     ✔ dplyr   0.8.1
## ✔ tidyr   0.8.3     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.4.0
## ── Conflicts ────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ tidyr::complete() masks RCurl::complete()
## ✖ dplyr::filter()   masks timeSeries::filter(), stats::filter()
## ✖ dplyr::first()    masks xts::first()
## ✖ dplyr::lag()      masks timeSeries::lag(), stats::lag()
## ✖ dplyr::last()     masks xts::last()
## ✖ dplyr::recode()   masks car::recode()
## ✖ purrr::reduce()   masks rugarch::reduce()
## ✖ purrr::some()     masks car::some()
# tron, neo, waves, nano: 
tron <- scrapData("tron")
## Warning in scrapData("tron"): pojawiły się wartości NA na skutek
## przekształcenia
summary(tron)
##       Date                 Open               High         
##  Min.   :2017-09-13   Min.   :0.001419   Min.   :0.001831  
##  1st Qu.:2018-03-12   1st Qu.:0.020352   1st Qu.:0.021185  
##  Median :2018-09-08   Median :0.025236   Median :0.026068  
##  Mean   :2018-09-08   Mean   :0.030960   Mean   :0.032981  
##  3rd Qu.:2019-03-07   3rd Qu.:0.037311   3rd Qu.:0.039448  
##  Max.   :2019-09-04   Max.   :0.224499   Max.   :0.300363  
##                                                            
##       Low               Close              Volume         
##  Min.   :0.001091   Min.   :0.001427   Min.   :2.648e+04  
##  1st Qu.:0.019608   1st Qu.:0.020355   1st Qu.:1.064e+08  
##  Median :0.024272   Median :0.025242   Median :2.107e+08  
##  Mean   :0.028860   Mean   :0.030956   Mean   :3.321e+08  
##  3rd Qu.:0.034994   3rd Qu.:0.037208   3rd Qu.:4.603e+08  
##  Max.   :0.176712   Max.   :0.220555   Max.   :4.089e+09  
##                                                           
##    MarketCap        
##  Min.   :1.860e+07  
##  1st Qu.:1.392e+09  
##  Median :1.689e+09  
##  Mean   :2.083e+09  
##  3rd Qu.:2.488e+09  
##  Max.   :1.450e+10  
##  NA's   :15
tron %>% as.tibble %>% tail
## Warning: `as.tibble()` is deprecated, use `as_tibble()` (but mind the new semantics).
## This warning is displayed once per session.
## # A tibble: 6 x 7
##   Date         Open   High    Low  Close    Volume  MarketCap
##   <date>      <dbl>  <dbl>  <dbl>  <dbl>     <dbl>      <dbl>
## 1 2019-08-30 0.0155 0.0158 0.0153 0.0156 439318790 1040672169
## 2 2019-08-31 0.0156 0.0157 0.0153 0.0156 467796844 1037558304
## 3 2019-09-01 0.0156 0.0157 0.0154 0.0156 424372234 1041912671
## 4 2019-09-02 0.0156 0.0161 0.0153 0.0159 458500298 1061853982
## 5 2019-09-03 0.0159 0.0161 0.0157 0.0159 422623093 1063540664
## 6 2019-09-04 0.0160 0.0160 0.0155 0.0156 407752910 1039747569
neo <- scrapData("neo")
## Warning in scrapData("neo"): pojawiły się wartości NA na skutek
## przekształcenia
summary(neo)
##       Date                 Open               High          
##  Min.   :2016-09-09   Min.   :  0.0802   Min.   :  0.08521  
##  1st Qu.:2017-06-08   1st Qu.:  1.4300   1st Qu.:  1.71000  
##  Median :2018-03-08   Median : 11.6800   Median : 12.12000  
##  Mean   :2018-03-08   Mean   : 24.5686   Mean   : 25.93194  
##  3rd Qu.:2018-12-05   3rd Qu.: 32.4900   3rd Qu.: 34.13500  
##  Max.   :2019-09-04   Max.   :187.9700   Max.   :196.85000  
##                                                             
##       Low                Close               Volume         
##  Min.   :  0.07229   Min.   :  0.08018   Min.   :1.560e+02  
##  1st Qu.:  1.30000   1st Qu.:  1.53000   1st Qu.:8.534e+06  
##  Median : 11.16000   Median : 11.69000   Median :9.640e+07  
##  Mean   : 22.95233   Mean   : 24.56408   Mean   :1.484e+08  
##  3rd Qu.: 30.29000   3rd Qu.: 32.28000   3rd Qu.:2.158e+08  
##  Max.   :151.46000   Max.   :187.40000   Max.   :1.664e+09  
##                                                             
##    MarketCap        
##  Min.   :5.292e+06  
##  1st Qu.:3.672e+08  
##  Median :8.342e+08  
##  Mean   :1.630e+09  
##  3rd Qu.:1.999e+09  
##  Max.   :1.218e+10  
##  NA's   :46
neo %>% as.tibble %>% tail
## # A tibble: 6 x 7
##   Date        Open  High   Low Close    Volume MarketCap
##   <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>     <dbl>
## 1 2019-08-30  8.77  8.92  8.69  8.76 214238128 618155575
## 2 2019-08-31  8.76  8.9   8.63  8.82 216947467 622362487
## 3 2019-09-01  8.82  8.89  8.69  8.8  202957135 620552317
## 4 2019-09-02  8.8   9.23  8.77  9.15 227666511 645598698
## 5 2019-09-03  9.15  9.37  9.09  9.3  241655994 655888043
## 6 2019-09-04  9.3   9.31  9.02  9.09 230107282 640861597
waves <- scrapData("waves")
## Warning in scrapData("waves"): pojawiły się wartości NA na skutek
## przekształcenia
summary(waves)
##       Date                 Open              High        
##  Min.   :2016-06-02   Min.   : 0.1275   Min.   : 0.1515  
##  1st Qu.:2017-03-26   1st Qu.: 0.4298   1st Qu.: 0.4512  
##  Median :2018-01-17   Median : 2.3800   Median : 2.4950  
##  Mean   :2018-01-17   Mean   : 2.8349   Mean   : 2.9856  
##  3rd Qu.:2018-11-10   3rd Qu.: 3.9275   3rd Qu.: 4.1625  
##  Max.   :2019-09-04   Max.   :16.1400   Max.   :18.0700  
##                                                          
##       Low              Close             Volume         
##  Min.   : 0.1227   Min.   : 0.1272   Min.   :     4912  
##  1st Qu.: 0.4140   1st Qu.: 0.4298   1st Qu.:   303484  
##  Median : 2.2900   Median : 2.3800   Median :  7525894  
##  Mean   : 2.6693   Mean   : 2.8322   Mean   : 13599345  
##  3rd Qu.: 3.6600   3rd Qu.: 3.9175   3rd Qu.: 20365275  
##  Max.   :14.5400   Max.   :16.0300   Max.   :139958633  
##                                                         
##    MarketCap        
##  Min.   :1.272e+07  
##  1st Qu.:4.116e+07  
##  Median :2.400e+08  
##  Mean   :2.860e+08  
##  3rd Qu.:3.968e+08  
##  Max.   :1.603e+09  
##  NA's   :18
waves %>% as.tibble %>% tail
## # A tibble: 6 x 7
##   Date        Open  High   Low Close   Volume MarketCap
##   <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>     <dbl>
## 1 2019-08-30  1.12  1.14  1.11  1.12  7948837 111875703
## 2 2019-08-31  1.12  1.14  1.08  1.11  8863325 110914531
## 3 2019-09-01  1.11  1.12  1.08  1.12  7635752 111861896
## 4 2019-09-02  1.12  1.16  1.1   1.15  9763244 115401844
## 5 2019-09-03  1.15  1.17  1.13  1.15 11907801 115202292
## 6 2019-09-04  1.15  1.16  1.11  1.13  9476856 113087466
nano <- scrapData("nano")
summary(nano)
##       Date                 Open               High         
##  Min.   :2017-03-07   Min.   : 0.00729   Min.   : 0.00795  
##  1st Qu.:2017-11-01   1st Qu.: 0.14547   1st Qu.: 0.15388  
##  Median :2018-06-13   Median : 1.19000   Median : 1.26000  
##  Mean   :2018-06-12   Mean   : 2.86186   Mean   : 3.09433  
##  3rd Qu.:2019-01-23   3rd Qu.: 2.42000   3rd Qu.: 2.58000  
##  Max.   :2019-09-04   Max.   :33.45000   Max.   :37.62000  
##       Low                Close              Volume         
##  Min.   : 0.006658   Min.   : 0.00729   Min.   :      663  
##  1st Qu.: 0.130532   1st Qu.: 0.14712   1st Qu.:   203958  
##  Median : 1.130000   Median : 1.19000   Median :  3746730  
##  Mean   : 2.581955   Mean   : 2.85865   Mean   : 10874235  
##  3rd Qu.: 2.290000   3rd Qu.: 2.42000   3rd Qu.: 11503800  
##  Max.   :29.630000   Max.   :33.70000   Max.   :396790016  
##    MarketCap        
##  Min.   :3.047e+05  
##  1st Qu.:1.661e+07  
##  Median :1.586e+08  
##  Mean   :3.804e+08  
##  3rd Qu.:3.224e+08  
##  Max.   :4.491e+09
nano %>% as.tibble %>% tail
## # A tibble: 6 x 7
##   Date        Open  High   Low Close  Volume MarketCap
##   <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>     <dbl>
## 1 2019-08-30 0.950 0.976 0.934 0.970 2748574 129266612
## 2 2019-08-31 0.970 0.976 0.935 0.958 2165914 127657898
## 3 2019-09-01 0.958 0.975 0.936 0.966 2162533 128736864
## 4 2019-09-02 0.966 1.02  0.958 1.01  2696596 135094914
## 5 2019-09-03 1.01  1.02  0.973 0.977 3499391 130168905
## 6 2019-09-04 0.977 0.983 0.958 0.970 2511865 129232255
###################################################################################################

tron$Date<- as.Date(tron$Date)
tron <- tron[, c("Date", "Close")]
colnames(tron) <- c("Date", "tron_closure")

neo$Date<- as.Date(neo$Date)
neo <- neo[, c("Date", "Close")]
colnames(neo) <- c("Date", "neo_closure")

waves$Date<- as.Date(waves$Date)
waves <- waves[, c("Date", "Close")]
colnames(waves) <- c("Date", "waves_closure")

nano$Date<- as.Date(nano$Date)
nano <- nano[, c("Date", "Close")]
colnames(nano) <- c("Date", "nano_closure")

tron <- tron[tron$Date >= "2019-01-01", ]
neo <- neo[neo$Date >= "2019-01-01", ]
waves <- waves[waves$Date >= "2019-01-01", ]
nano <- nano[nano$Date >= "2019-01-01", ]

library(xts)
tron$rtron <- diff.xts(log(tron$tron))
neo$rneo <- diff.xts(log(neo$neo))
waves$rwaves <- diff.xts(log(waves$waves))
nano$rnano <- diff.xts(log(nano$nano))

Następnym krokiem było utworzenie portfela oraz wstępna analiza zgromadzonych danych.

df <- data.frame(tron$Date, tron$rtron, neo$rneo, waves$rwaves, nano$rnano)
colnames(df) <- c("Date", "tron", "neo", "waves", "nano")


df$portfel <- rowSums(df[,c(2:5)])
portfel <- df[,c(1,6)]

colnames(portfel) <- c("Date", "zwroty")
  1. Wykres gęstości zwrotów poszczególnych kryptowalut:
#install.packages("reshape",repos = "http://cran.us.r-project.org")
library(reshape)
## 
## Attaching package: 'reshape'
## The following object is masked from 'package:dplyr':
## 
##     rename
## The following objects are masked from 'package:tidyr':
## 
##     expand, smiths
molten.data <- melt(df,
                    id = c("Date"))
ggplot(molten.data,
       aes(x = value, fill = variable)) +
  geom_density(alpha = .3) +
  xlab ("") + ylab("gęstość") +
  ggtitle("Gęstości zwrotów poszczególnych kryptowalut")
## Warning: Removed 5 rows containing non-finite values (stat_density).

2. Badanie niestacjonarność poszczególnych szeregów czasowych.

# tron, neo, waves, nano 

par(mfrow = c(2, 1))
plot(tron$Date, tron$rtron,
     type = "l", col="red", lwd = 1,
     main = "zwroty TRON")
plot(tron$Date, tron$tron,
     type = "l", col = "black", lwd = 1,
     main = "notowania TRON")

par(mfrow = c(1, 1))

par(mfrow = c(2, 1))
plot(neo$Date, neo$rneo,
     type = "l", col="red", lwd = 1,
     main = "zwroty NEO")
plot(neo$Date, neo$neo,
     type = "l", col = "black", lwd = 1,
     main = "notowania NEO")

par(mfrow = c(1, 1))

par(mfrow = c(2, 1))
plot(waves$Date, waves$rwaves,
     type = "l", col="red", lwd = 1,
     main = "zwroty WAVES")
plot(waves$Date, waves$waves,
     type = "l", col = "black", lwd = 1,
     main = "notowania WAVES")

par(mfrow = c(1, 1))

par(mfrow = c(2, 1))
plot(nano$Date, nano$rnano,
     type = "l", col="red", lwd = 1,
     main = "zwroty NANO")
plot(nano$Date, nano$nano,
     type = "l", col = "black", lwd = 1,
     main = "notowania NANO")

par(mfrow = c(1, 1))

Na podstawie wykresów trudno stwierdzić stacjonarność szeregów. Wydaje się, że średnia może mieć stałą wartość oczekiwaną, natomiast ciężko to powiedzieć o wariancji.

  1. Testowanie niestacjonarność poszczególnych szeregów czasowych.
testdf <- function(variable, adf_order) {
    results_adf <- data.frame(order = -1,
                              adf = 0,
                              p_adf = "",
                              bgodfrey = 0, p_bg = 0)
    variable <- variable[!is.na(variable)]

    for (order in 0:adf_order) {
        df.test_ <- ur.df(variable, type = c("drift"), lags = order)
        df_ <- df.test_@teststat[1]
        df_crit <- df.test_@cval[1, ]
        df_crit <- (df_ < df_crit) * 1
        p_adf <- ifelse(sum(df_crit) == 0,
                        ">10pct",
                        paste("<",
                              names(df_crit)[min(which(df_crit == 1))],
                              sep = "")
                        )

        resids_ <- df.test_@testreg$residuals
        bgtest_ <- bgtest(resids_ ~ 1, order = 1)
        bgodfrey <- bgtest_$statistic
        names(bgodfrey) <- NULL
        p_bg <- bgtest_$p.value

        results_adf <- rbind(results_adf,
                             data.frame(order = order,
                                        adf = df_,
                                        p_adf = p_adf,
                                        bgodfrey = bgodfrey,
                                        p_bg = p_bg)
                             )
    }

    results_adf <- results_adf[results_adf$order >= 0, ]

    plot(variable,
         type = "l",
         col = "blue",
         lwd = 2,
         main = "Plot of the examined variable")

    return(results_adf)
}

# tron, neo, waves, nano

df.test <- ur.df(tron$tron, type = c("drift"), lags = 0)
summary(df.test)
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression drift 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1)
## 
## Residuals:
##        Min         1Q     Median         3Q        Max 
## -0.0041952 -0.0005973 -0.0000843  0.0005849  0.0046388 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)  
## (Intercept)  0.0008251  0.0004601   1.793   0.0742 .
## z.lag.1     -0.0326734  0.0175531  -1.861   0.0639 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.001353 on 244 degrees of freedom
## Multiple R-squared:  0.014,  Adjusted R-squared:  0.00996 
## F-statistic: 3.465 on 1 and 244 DF,  p-value: 0.06389
## 
## 
## Value of test-statistic is: -1.8614 1.7499 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.46 -2.88 -2.57
## phi1  6.52  4.63  3.81
df.test2 <- ur.df(neo$neo, type = c("drift"), lags = 0)
summary(df.test2)
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression drift 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.0792 -0.2702  0.0014  0.2529  3.5368 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept)  0.30167    0.16078   1.876   0.0618 .
## z.lag.1     -0.02769    0.01454  -1.904   0.0581 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.6199 on 244 degrees of freedom
## Multiple R-squared:  0.01464,    Adjusted R-squared:  0.0106 
## F-statistic: 3.625 on 1 and 244 DF,  p-value: 0.0581
## 
## 
## Value of test-statistic is: -1.9039 1.8203 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.46 -2.88 -2.57
## phi1  6.52  4.63  3.81
df.test3 <- ur.df(waves$waves, type = c("drift"), lags = 0)
summary(df.test3)
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression drift 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.39195 -0.04980 -0.00308  0.04624  0.73916 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  0.01970    0.02681   0.735    0.463
## z.lag.1     -0.01218    0.01132  -1.076    0.283
## 
## Residual standard error: 0.1039 on 244 degrees of freedom
## Multiple R-squared:  0.004722,   Adjusted R-squared:  0.0006428 
## F-statistic: 1.158 on 1 and 244 DF,  p-value: 0.283
## 
## 
## Value of test-statistic is: -1.0759 1.3551 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.46 -2.88 -2.57
## phi1  6.52  4.63  3.81
df.test4 <- ur.df(nano$nano, type = c("drift"), lags = 0)
summary(df.test4)
## 
## ############################################### 
## # Augmented Dickey-Fuller Test Unit Root Test # 
## ############################################### 
## 
## Test regression drift 
## 
## 
## Call:
## lm(formula = z.diff ~ z.lag.1 + 1)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.17680 -0.03384 -0.00477  0.03155  0.29804 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept)  0.02983    0.01764   1.691   0.0921 .
## z.lag.1     -0.02445    0.01403  -1.743   0.0826 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.06851 on 244 degrees of freedom
## Multiple R-squared:  0.01229,    Adjusted R-squared:  0.008246 
## F-statistic: 3.037 on 1 and 244 DF,  p-value: 0.08265
## 
## 
## Value of test-statistic is: -1.7427 1.5185 
## 
## Critical values for test statistics: 
##       1pct  5pct 10pct
## tau2 -3.46 -2.88 -2.57
## phi1  6.52  4.63  3.81

P-value testu jest większe od 5% w każdym przypadku. Zatem brak jest podstaw do odrzucenia H0 o niestacjonarności szeregów.

Analiza portfela zrównoważonego

  1. Wykres zwrotów z portfela. ```
plot(portfel$Date, portfel$zwroty,
     type = "l", col="blue", lwd = 1,
     main = "zwroty z portfela")

  1. Testowanie normalności rozkładu zwrotów.

histogram zwrotów z portfela

library(fGarch)
bstats <- basicStats(portfel$zwroty)
knitr::kable(as.matrix(bstats), digits = 2)
X..portfel.zwroty
nobs 247.00
NAs 1.00
Minimum -0.53
Maximum 0.54
1. Quartile -0.08
3. Quartile 0.08
Mean 0.00
Median 0.00
Sum -1.10
SE Mean 0.01
LCL Mean -0.02
UCL Mean 0.02
Variance 0.03
Stdev 0.16
Skewness -0.22
Kurtosis 1.48
hist(portfel$zwroty, prob = T, breaks = 40)
curve(dnorm(x, mean = mean(portfel$zwroty, na.rm = T),
            sd  = sd(portfel$zwroty, na.rm = T)),
      col = "blue", lwd = 2, add = TRUE)

# test jarque- bera

jarque.bera.test <-
  function(x)
  {
    if((NCOL(x) > 1) || is.data.frame(x))
      stop("x is not a vector or univariate time series")
    if(any(is.na(x)))
      stop("NAs in x")
    DNAME <- deparse(substitute(x))
    n <- length(x)
    m1 <- sum(x)/n
    m2 <- sum((x-m1)^2)/n
    m3 <- sum((x-m1)^3)/n
    m4 <- sum((x-m1)^4)/n
    b1 <- (m3/m2^(3/2))^2
    b2 <- (m4/m2^2)
    STATISTIC <- n*b1/6+n*(b2-3)^2/24
    PVAL <- 1 - pchisq(STATISTIC,df = 2)
    PARAMETER <- 2
    METHOD <- "Jarque Bera Test"
    names(STATISTIC) <- "X-squared"
    names(PARAMETER) <- "df"
    structure(list(statistic = STATISTIC,
                   parameter = PARAMETER,
                   p.value = PVAL,
                   method = METHOD,
                   data.name = DNAME),
              class = "htest")
  }
jarque.bera.test(na.omit(portfel$zwroty))
## 
##  Jarque Bera Test
## 
## data:  na.omit(portfel$zwroty)
## X-squared = 25.598, df = 2, p-value = 2.764e-06
durbinWatsonTest(lm(portfel$zwroty ~ 1),
                 max.lag = 5)
##  lag Autocorrelation D-W Statistic p-value
##    1     -0.11575977      2.230080   0.066
##    2     -0.07875211      2.154012   0.192
##    3      0.02196341      1.949000   0.732
##    4     -0.03211117      2.056394   0.540
##    5      0.02745487      1.927502   0.788
##  Alternative hypothesis: rho[lag] != 0

Badanie normalności reszt - test Jarque Bera Badanie autokorelacji stóp zwrotów- statystyki Durbina-Watsona

Odrzucamy założenie o normalności reszt. P-value dla wszystkich opóźnień przekracza poziom istotności 5%, brak podstaw do odrzucenia H0 o braku autokorelacji zwrotów.

  1. Testowanie występowania efektów ARCH
ArchTest <- function (x, lags=12, demean = FALSE) 
{
  # Capture name of x for documentation in the output  
  xName <- deparse(substitute(x))
  # 
  x <- as.vector(x)
  if(demean) x <- scale(x, center = TRUE, scale = FALSE)
  #  
  lags <- lags + 1
  mat <- embed(x^2, lags)
  arch.lm <- summary(lm(mat[, 1] ~ mat[, -1]))
  STATISTIC <- arch.lm$r.squared * length(resid(arch.lm))
  names(STATISTIC) <- "Chi-squared"
  PARAMETER <- lags - 1
  names(PARAMETER) <- "df"
  PVAL <- 1 - pchisq(STATISTIC, df = PARAMETER)
  METHOD <- "ARCH LM-test;  Null hypothesis:  no ARCH effects"
  result <- list(statistic = STATISTIC, parameter = PARAMETER, 
                 p.value = PVAL, method = METHOD, data.name =
                   xName)
  class(result) <- "htest"
  return(result)
}
ArchTest(portfel$zwroty, lags = 5)
## 
##  ARCH LM-test; Null hypothesis: no ARCH effects
## 
## data:  portfel$zwroty
## Chi-squared = 14.797, df = 5, p-value = 0.01126

Odrzucamy H0 o braku efektów ARCH.

  1. Sprawdzenie, czy ACF reszt i ich kwadratów zachowują się jak biały szum.

Wykresy ACF dla zwrotów i kwadratów zwrotów.

acf(portfel$zwroty, lag.max = 36, na.action = na.pass,
    col = "blue", lwd = 7,
    main = "Wykres ACF zwrotów portfela")

acf(portfel$zwroty^2, lag.max = 100, na.action = na.pass,
    col = "blue", lwd = 7,
    main = "Wykres ACF kwadratów zwrotów portfela")

Na podstawie wykresów wnioskujemy, że nie zachowują się jak biały szum.

  1. Badanie autokorelacji zmiennej

Wykres wartości ACF dla zwrotów: Badana zmienna podlega autokorelacji.

Estymacja modelów GARCH w celu wyboru odpowiedniego modelu

Tworzymy próbkę ograniczoną do pewnego okresu czasu. Generujemy GARCH o różnych rzędach p i q:

portfel_probka_in <- portfel[portfel$Date <= as.Date("2019-05-01"), ]
portfel_probka_in <- portfel_probka_in[as.Date("2019-03-01") <= portfel_probka_in$Date, ]

library(fGarch)
spec_11 <- ugarchspec(variance.model = list(model = "sGARCH",
                                         garchOrder = c(1, 1)),
                   mean.model = list(armaOrder = c(0, 0),
                                     include.mean = T),
                   distribution.model = "norm")

garch_11 <- ugarchfit(spec = spec_11,
                             data = na.omit(portfel_probka_in$zwroty))
## Warning in .sgarchfit(spec = spec, data = data, out.sample = out.sample, : 
## ugarchfit-->waring: using less than 100 data
##  points for estimation
spec_10 <- ugarchspec(variance.model = list(model = "sGARCH",
                                         garchOrder = c(1, 0)),
                   mean.model = list(armaOrder = c(0, 0),
                                     include.mean = T),
                   distribution.model = "norm")

garch_10 <- ugarchfit(spec = spec_10,
                             data = na.omit(portfel_probka_in$zwroty))
## Warning in .sgarchfit(spec = spec, data = data, out.sample = out.sample, : 
## ugarchfit-->waring: using less than 100 data
##  points for estimation
spec_20 <- ugarchspec(variance.model = list(model = "sGARCH",
                                         garchOrder = c(2, 0)),
                   mean.model = list(armaOrder = c(0, 0),
                                     include.mean = T),
                   distribution.model = "norm")

garch_20 <- ugarchfit(spec = spec_20,
                             data = na.omit(portfel_probka_in$zwroty))
## Warning in .sgarchfit(spec = spec, data = data, out.sample = out.sample, : 
## ugarchfit-->waring: using less than 100 data
##  points for estimation
spec_21 <- ugarchspec(variance.model = list(model = "sGARCH",
                                         garchOrder = c(2, 1)),
                   mean.model = list(armaOrder = c(0, 0),
                                     include.mean = T),
                   distribution.model = "norm")

garch_21 <- ugarchfit(spec = spec_21,
                             data = na.omit(portfel_probka_in$zwroty))
## Warning in .sgarchfit(spec = spec, data = data, out.sample = out.sample, : 
## ugarchfit-->waring: using less than 100 data
##  points for estimation
spec_30 <- ugarchspec(variance.model = list(model = "sGARCH",
                                         garchOrder = c(3, 0)),
                   mean.model = list(armaOrder = c(0, 0),
                                     include.mean = T),
                   distribution.model = "norm")

garch_30 <- ugarchfit(spec = spec_30,
                             data = na.omit(portfel_probka_in$zwroty))
## Warning in .sgarchfit(spec = spec, data = data, out.sample = out.sample, : 
## ugarchfit-->waring: using less than 100 data
##  points for estimation
spec_31 <- ugarchspec(variance.model = list(model = "sGARCH",
                                         garchOrder = c(3, 1)),
                   mean.model = list(armaOrder = c(0, 0),
                                     include.mean = T),
                   distribution.model = "norm")

garch_31 <- ugarchfit(spec = spec_31,
                             data = na.omit(portfel_probka_in$zwroty))
## Warning in .sgarchfit(spec = spec, data = data, out.sample = out.sample, : 
## ugarchfit-->waring: using less than 100 data
##  points for estimation
garch_11
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : sGARCH(1,1)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.007806    0.017410 0.448338  0.65391
## omega   0.000105    0.001974 0.053073  0.95767
## alpha1  0.000000    0.048064 0.000000  1.00000
## beta1   0.999000    0.127840 7.814467  0.00000
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.007806    0.011901 0.655892 0.511894
## omega   0.000105    0.003070 0.034119 0.972782
## alpha1  0.000000    0.114887 0.000000 1.000000
## beta1   0.999000    0.242399 4.121296 0.000038
## 
## LogLikelihood : 37.53098 
## 
## Information Criteria
## ------------------------------------
##                      
## Akaike       -1.08164
## Bayes        -0.94441
## Shibata      -1.08932
## Hannan-Quinn -1.02776
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      1.332  0.2484
## Lag[2*(p+q)+(p+q)-1][2]     1.379  0.3901
## Lag[4*(p+q)+(p+q)-1][5]     2.567  0.4917
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                    0.01737  0.8951
## Lag[2*(p+q)+(p+q)-1][5]   0.75792  0.9116
## Lag[4*(p+q)+(p+q)-1][9]   2.60867  0.8214
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3]   0.07056 0.500 2.000  0.7905
## ARCH Lag[5]   0.45740 1.440 1.667  0.8961
## ARCH Lag[7]   1.19966 2.315 1.543  0.8791
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  2.6032
## Individual Statistics:              
## mu     0.16164
## omega  0.05206
## alpha1 0.05538
## beta1  0.04945
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.07 1.24 1.6
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias           0.2212 0.8257    
## Negative Sign Bias  0.1040 0.9175    
## Positive Sign Bias  0.1687 0.8666    
## Joint Effect        0.0537 0.9967    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     14.77       0.7368
## 2    30     26.06       0.6220
## 3    40     27.03       0.9262
## 4    50     46.06       0.5929
## 
## 
## Elapsed time : 0.10078
garch_10
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : sGARCH(1,0)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error   t value Pr(>|t|)
## mu      0.022708    0.000035   655.396        0
## omega   0.000046    0.000000 12725.898        0
## alpha1  0.998988    0.018431    54.201        0
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.022708    0.000033  683.288        0
## omega   0.000046    0.000000 4755.550        0
## alpha1  0.998988    0.017693   56.461        0
## 
## LogLikelihood : -171.9577 
## 
## Information Criteria
## ------------------------------------
##                    
## Akaike       5.6438
## Bayes        5.7467
## Shibata      5.6394
## Hannan-Quinn 5.6842
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                    0.01618  0.8988
## Lag[2*(p+q)+(p+q)-1][2]   0.04087  0.9635
## Lag[4*(p+q)+(p+q)-1][5]   0.65108  0.9321
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                    0.04859  0.8255
## Lag[2*(p+q)+(p+q)-1][2]   0.07430  0.9381
## Lag[4*(p+q)+(p+q)-1][5]   0.12826  0.9969
## d.o.f=1
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[2]   0.04821 0.500 2.000  0.8262
## ARCH Lag[4]   0.08131 1.397 1.611  0.9879
## ARCH Lag[6]   0.11194 2.222 1.500  0.9988
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  4.1168
## Individual Statistics:             
## mu     0.1849
## omega  2.9172
## alpha1 3.2082
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          0.846 1.01 1.35
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias           1.3815 0.1725    
## Negative Sign Bias  1.6449 0.1055    
## Positive Sign Bias  0.1268 0.8995    
## Joint Effect        3.4274 0.3303    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     45.74    5.391e-04
## 2    30     64.77    1.518e-04
## 3    40    105.74    4.528e-08
## 4    50    141.23    7.130e-11
## 
## 
## Elapsed time : 0.09729886
garch_20
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : sGARCH(2,0)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.006944    0.017099 0.406135 0.684644
## omega   0.018196    0.003669 4.959760 0.000001
## alpha1  0.000000    0.100299 0.000002 0.999998
## alpha2  0.000000    0.082562 0.000000 1.000000
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.006944    0.015672 0.443123 0.657677
## omega   0.018196    0.005628 3.233144 0.001224
## alpha1  0.000000    0.054268 0.000004 0.999997
## alpha2  0.000000    0.044318 0.000000 1.000000
## 
## LogLikelihood : 37.12038 
## 
## Information Criteria
## ------------------------------------
##                      
## Akaike       -1.06840
## Bayes        -0.93116
## Shibata      -1.07607
## Hannan-Quinn -1.01452
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      1.231  0.2673
## Lag[2*(p+q)+(p+q)-1][2]     1.276  0.4168
## Lag[4*(p+q)+(p+q)-1][5]     2.506  0.5045
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                   0.007693  0.9301
## Lag[2*(p+q)+(p+q)-1][5]  0.720749  0.9189
## Lag[4*(p+q)+(p+q)-1][9]  2.565156  0.8280
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3]   0.07805 0.500 2.000  0.7800
## ARCH Lag[5]   0.38792 1.440 1.667  0.9161
## ARCH Lag[7]   1.14774 2.315 1.543  0.8884
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  1.4985
## Individual Statistics:             
## mu     0.1769
## omega  0.1721
## alpha1 0.1867
## alpha2 0.7394
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.07 1.24 1.6
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias          0.22974 0.8191    
## Negative Sign Bias 0.16873 0.8666    
## Positive Sign Bias 0.17575 0.8611    
## Joint Effect       0.06553 0.9956    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     13.48       0.8129
## 2    30     28.97       0.4667
## 3    40     38.65       0.4859
## 4    50     47.68       0.5268
## 
## 
## Elapsed time : 0.138262
garch_21
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : sGARCH(2,1)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.007841    0.022847 0.343175 0.731467
## omega   0.000110    0.007705 0.014233 0.988644
## alpha1  0.000000    0.560631 0.000000 1.000000
## alpha2  0.000000    0.407393 0.000000 1.000000
## beta1   0.999000    0.515859 1.936575 0.052797
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.007841    0.115623 0.067812  0.94593
## omega   0.000110    0.059377 0.001847  0.99853
## alpha1  0.000000    4.281730 0.000000  1.00000
## alpha2  0.000000    2.990288 0.000000  1.00000
## beta1   0.999000    4.022204 0.248371  0.80385
## 
## LogLikelihood : 37.55217 
## 
## Information Criteria
## ------------------------------------
##                      
## Akaike       -1.05007
## Bayes        -0.87853
## Shibata      -1.06183
## Hannan-Quinn -0.98272
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      1.338  0.2473
## Lag[2*(p+q)+(p+q)-1][2]     1.385  0.3886
## Lag[4*(p+q)+(p+q)-1][5]     2.571  0.4909
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                          statistic p-value
## Lag[1]                     0.01796  0.8934
## Lag[2*(p+q)+(p+q)-1][8]    1.52685  0.9241
## Lag[4*(p+q)+(p+q)-1][14]   5.92899  0.6484
## d.o.f=3
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[4]    0.2241 0.500 2.000  0.6360
## ARCH Lag[6]    0.9465 1.461 1.711  0.7632
## ARCH Lag[8]    1.6141 2.368 1.583  0.8180
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  4.2465
## Individual Statistics:              
## mu     0.16080
## omega  0.04962
## alpha1 0.05764
## alpha2 0.05215
## beta1  0.04731
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.28 1.47 1.88
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias          0.22079 0.8260    
## Negative Sign Bias 0.10085 0.9200    
## Positive Sign Bias 0.16849 0.8668    
## Joint Effect       0.05345 0.9968    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     14.77       0.7368
## 2    30     26.06       0.6220
## 3    40     27.03       0.9262
## 4    50     46.06       0.5929
## 
## 
## Elapsed time : 0.07806802
garch_30
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : sGARCH(3,0)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.005816    0.017220 0.337721 0.735574
## omega   0.018070    0.003934 4.593297 0.000004
## alpha1  0.000000    0.103416 0.000002 0.999999
## alpha2  0.000000    0.082420 0.000000 1.000000
## alpha3  0.022437    0.079692 0.281545 0.778293
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.005816    0.016204 0.358897 0.719672
## omega   0.018070    0.006183 2.922281 0.003475
## alpha1  0.000000    0.058071 0.000003 0.999998
## alpha2  0.000000    0.045824 0.000000 1.000000
## alpha3  0.022437    0.045603 0.492005 0.622716
## 
## LogLikelihood : 37.18918 
## 
## Information Criteria
## ------------------------------------
##                      
## Akaike       -1.03836
## Bayes        -0.86682
## Shibata      -1.05012
## Hannan-Quinn -0.97101
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      1.224  0.2686
## Lag[2*(p+q)+(p+q)-1][2]     1.272  0.4179
## Lag[4*(p+q)+(p+q)-1][5]     2.494  0.5072
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                          statistic p-value
## Lag[1]                    0.003594  0.9522
## Lag[2*(p+q)+(p+q)-1][8]   1.269796  0.9518
## Lag[4*(p+q)+(p+q)-1][14]  5.454479  0.7115
## d.o.f=3
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[4]    0.1775 0.500 2.000  0.6735
## ARCH Lag[6]    0.7457 1.461 1.711  0.8215
## ARCH Lag[8]    1.3605 2.368 1.583  0.8652
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  1.5763
## Individual Statistics:              
## mu     0.17970
## omega  0.14824
## alpha1 0.17388
## alpha2 0.75575
## alpha3 0.08148
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.28 1.47 1.88
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias          0.28404 0.7774    
## Negative Sign Bias 0.23007 0.8189    
## Positive Sign Bias 0.17455 0.8621    
## Joint Effect       0.09603 0.9923    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     14.77       0.7368
## 2    30     24.13       0.7225
## 3    40     38.65       0.4859
## 4    50     42.84       0.7199
## 
## 
## Elapsed time : 0.167423
garch_31
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : sGARCH(3,1)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.007881    0.020894  0.37720  0.70603
## omega   0.000115    0.009396  0.01221  0.99026
## alpha1  0.000000    0.658353  0.00000  1.00000
## alpha2  0.000000    0.747821  0.00000  1.00000
## alpha3  0.000000    0.280928  0.00000  1.00000
## beta1   0.999000    0.622721  1.60425  0.10866
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.007881    0.222268 0.035458  0.97171
## omega   0.000115    0.076655 0.001497  0.99881
## alpha1  0.000000    5.404559 0.000000  1.00000
## alpha2  0.000000    6.070504 0.000000  1.00000
## alpha3  0.000000    2.118972 0.000000  1.00000
## beta1   0.999000    5.036373 0.198357  0.84277
## 
## LogLikelihood : 37.57291 
## 
## Information Criteria
## ------------------------------------
##                      
## Akaike       -1.01848
## Bayes        -0.81263
## Shibata      -1.03510
## Hannan-Quinn -0.93766
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      1.345  0.2462
## Lag[2*(p+q)+(p+q)-1][2]     1.392  0.3870
## Lag[4*(p+q)+(p+q)-1][5]     2.575  0.4900
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                          statistic p-value
## Lag[1]                     0.01856  0.8916
## Lag[2*(p+q)+(p+q)-1][11]   4.24114  0.6966
## Lag[4*(p+q)+(p+q)-1][19]   8.05072  0.6700
## d.o.f=4
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[5]    0.3576 0.500 2.000  0.5498
## ARCH Lag[7]    1.4420 1.473 1.746  0.6377
## ARCH Lag[9]    5.3536 2.402 1.619  0.2314
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  4.3685
## Individual Statistics:              
## mu     0.15997
## omega  0.04751
## alpha1 0.05929
## alpha2 0.05413
## alpha3 0.04400
## beta1  0.04548
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.49 1.68 2.12
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias          0.22029 0.8264    
## Negative Sign Bias 0.09757 0.9226    
## Positive Sign Bias 0.16823 0.8670    
## Joint Effect       0.05320 0.9968    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     12.84       0.8467
## 2    30     23.16       0.7691
## 3    40     23.16       0.9793
## 4    50     42.84       0.7199
## 
## 
## Elapsed time : 0.07807183

Dokonujemy porównania modeli uwzględniając następujące kryteria: AIC, BIC, Shibata oraz Hannan-Quinna.

``` -garch_31 Akaike -1.01848 Bayes -0.81263 Shibata -1.03510 Hannan-Quinn -0.93766

-garch_30 Akaike -1.03836 Bayes -0.86682 Shibata -1.05012 Hannan-Quinn -0.97101

-garch_21 Akaike -1.05007 Bayes -0.87853 Shibata -1.06183 Hannan-Quinn -0.98272

-garch_20 Akaike -1.06840 Bayes -0.93116 Shibata -1.07607 Hannan-Quinn -1.01452

-garch_10 Akaike 5.6438 Bayes 5.7467 Shibata 5.6394 Hannan-Quinn 5.6842

-garch_11 Akaike -1.08164 Bayes -0.94441 Shibata -1.08932 Hannan-Quinn -1.02776

Najniższe kryteria przyjmuje model GARCH(1,1).

garch_11
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : sGARCH(1,1)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.007806    0.017410 0.448338  0.65391
## omega   0.000105    0.001974 0.053073  0.95767
## alpha1  0.000000    0.048064 0.000000  1.00000
## beta1   0.999000    0.127840 7.814467  0.00000
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.007806    0.011901 0.655892 0.511894
## omega   0.000105    0.003070 0.034119 0.972782
## alpha1  0.000000    0.114887 0.000000 1.000000
## beta1   0.999000    0.242399 4.121296 0.000038
## 
## LogLikelihood : 37.53098 
## 
## Information Criteria
## ------------------------------------
##                      
## Akaike       -1.08164
## Bayes        -0.94441
## Shibata      -1.08932
## Hannan-Quinn -1.02776
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      1.332  0.2484
## Lag[2*(p+q)+(p+q)-1][2]     1.379  0.3901
## Lag[4*(p+q)+(p+q)-1][5]     2.567  0.4917
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                    0.01737  0.8951
## Lag[2*(p+q)+(p+q)-1][5]   0.75792  0.9116
## Lag[4*(p+q)+(p+q)-1][9]   2.60867  0.8214
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3]   0.07056 0.500 2.000  0.7905
## ARCH Lag[5]   0.45740 1.440 1.667  0.8961
## ARCH Lag[7]   1.19966 2.315 1.543  0.8791
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  2.6032
## Individual Statistics:              
## mu     0.16164
## omega  0.05206
## alpha1 0.05538
## beta1  0.04945
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.07 1.24 1.6
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias           0.2212 0.8257    
## Negative Sign Bias  0.1040 0.9175    
## Positive Sign Bias  0.1687 0.8666    
## Joint Effect        0.0537 0.9967    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     14.77       0.7368
## 2    30     26.06       0.6220
## 3    40     27.03       0.9262
## 4    50     46.06       0.5929
## 
## 
## Elapsed time : 0.10078

Testy wskazują na to, że nie występuje autokorelacja reszt i kwadratów reszt wystandaryzowanych, ani efekty ARCH.

Następnie utworzono wykresy funkcji autokorelacji dla kwadratów reszt:

plot(garch_11, which = 10)

plot(garch_20, which = 10)

plot(garch_10, which = 10)

plot(garch_21, which = 10)

plot(garch_30, which = 10)

plot(garch_31, which = 10)

Wypustki są nieistotne, ponieważ nie przekraczają obszaru krytycznego. Zatem nie występuje autokorelacja reszt.

Kolejnym krokiem jest obliczenie Value-At_Risk w in sample i out of sample oraz porównanie kwantyli empirycznych ze standardowym z rozkładu normalnego.

portfel_probka_in$zwrotystd <- (portfel_probka_in$zwroty - mean(portfel_probka_in$zwroty, na.rm=T)) /
  sd(portfel_probka_in$zwroty ,na.rm = T)
tail(portfel_probka_in)
##           Date      zwroty  zwrotystd
## 116 2019-04-26 -0.02431955 -0.2332355
## 117 2019-04-27 -0.01574112 -0.1692550
## 118 2019-04-28 -0.06799414 -0.5589739
## 119 2019-04-29 -0.16912923 -1.3132701
## 120 2019-04-30  0.29343999  2.1367114
## 121 2019-05-01 -0.06367050 -0.5267269
basicStats(portfel_probka_in$zwrotystd)
##             X..portfel_probka_in.zwrotystd
## nobs                             62.000000
## NAs                               0.000000
## Minimum                          -2.831300
## Maximum                           3.986365
## 1. Quartile                      -0.510065
## 3. Quartile                       0.520829
## Mean                              0.000000
## Median                           -0.059323
## Sum                               0.000000
## SE Mean                           0.127000
## LCL Mean                         -0.253952
## UCL Mean                          0.253952
## Variance                          1.000000
## Stdev                             1.000000
## Skewness                          0.632851
## Kurtosis                          3.294645
q01 <- quantile(portfel_probka_in$zwrotystd, 0.01, na.rm = T)
q01
##        1% 
## -2.297398
qnorm(0.01, 0, 1)
## [1] -2.326348
str(garch_11)
## Formal class 'uGARCHfit' [package "rugarch"] with 2 slots
##   ..@ fit  :List of 27
##   .. ..$ hessian        : num [1:4, 1:4] 3593 -23556 -810 -522 -23556 ...
##   .. ..$ cvar           : num [1:4, 1:4] 3.03e-04 4.57e-06 2.26e-04 -3.87e-04 4.57e-06 ...
##   .. ..$ var            : num [1:62] 0.0177 0.0178 0.0179 0.0179 0.018 ...
##   .. ..$ sigma          : num [1:62] 0.133 0.133 0.134 0.134 0.134 ...
##   .. ..$ condH          : num 6.19
##   .. ..$ z              : num [1:62] 0.293 -0.437 -0.419 -0.685 1.064 ...
##   .. ..$ LLH            : num 37.5
##   .. ..$ log.likelihoods: num [1:62] -1.056 -1.001 -1.006 -0.856 -0.522 ...
##   .. ..$ residuals      : num [1:62] 0.0389 -0.0582 -0.056 -0.0918 0.143 ...
##   .. ..$ coef           : Named num [1:4] 7.81e-03 1.05e-04 3.05e-09 9.99e-01
##   .. .. ..- attr(*, "names")= chr [1:4] "mu" "omega" "alpha1" "beta1"
##   .. ..$ robust.cvar    : num [1:4, 1:4] 1.42e-04 9.19e-06 4.07e-04 -7.66e-04 9.19e-06 ...
##   .. ..$ A              : num [1:4, 1:4] 57.95 -379.93 -13.07 -8.42 -379.93 ...
##   .. ..$ B              : num [1:4, 1:4] 37.2 3032.2 12.8 57.3 3032.2 ...
##   .. ..$ scores         : num [1:62, 1:4] -2.16 3.31 3.18 5.14 -7.93 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : NULL
##   .. .. .. ..$ : chr [1:4] "mu" "omega" "alpha1" "beta1"
##   .. ..$ se.coef        : num [1:4] 0.01741 0.00197 0.04806 0.12784
##   .. ..$ tval           : Named num [1:4] 4.48e-01 5.31e-02 6.34e-08 7.81
##   .. .. ..- attr(*, "names")= chr [1:4] "mu" "omega" "alpha1" "beta1"
##   .. ..$ matcoef        : num [1:4, 1:4] 7.81e-03 1.05e-04 3.05e-09 9.99e-01 1.74e-02 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:4] "mu" "omega" "alpha1" "beta1"
##   .. .. .. ..$ : chr [1:4] " Estimate" " Std. Error" " t value" "Pr(>|t|)"
##   .. ..$ robust.se.coef : num [1:4] 0.0119 0.00307 0.11489 0.2424
##   .. ..$ robust.tval    : Named num [1:4] 6.56e-01 3.41e-02 2.65e-08 4.12
##   .. .. ..- attr(*, "names")= chr [1:4] "mu" "omega" "alpha1" "beta1"
##   .. ..$ robust.matcoef : num [1:4, 1:4] 7.81e-03 1.05e-04 3.05e-09 9.99e-01 1.19e-02 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:4] "mu" "omega" "alpha1" "beta1"
##   .. .. .. ..$ : chr [1:4] " Estimate" " Std. Error" " t value" "Pr(>|t|)"
##   .. ..$ fitted.values  : num [1:62] 0.00781 0.00781 0.00781 0.00781 0.00781 ...
##   .. ..$ convergence    : num 0
##   .. ..$ kappa          : num 1
##   .. ..$ persistence    : num 0.999
##   .. ..$ timer          : 'difftime' num 0.100780010223389
##   .. .. ..- attr(*, "units")= chr "secs"
##   .. ..$ ipars          : num [1:19, 1:6] 0.00781 0 0 0 0 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:19] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:6] "Level" "Fixed" "Include" "Estimate" ...
##   .. ..$ solver         :List of 2
##   .. .. ..$ sol :List of 10
##   .. .. .. ..$ pars       : Named num [1:4] 7.81e-03 1.05e-04 3.05e-09 9.99e-01
##   .. .. .. .. ..- attr(*, "names")= chr [1:4] "mu" "omega" "alpha1" "beta1"
##   .. .. .. ..$ convergence: num 0
##   .. .. .. ..$ values     : num [1:3] -10.5 -37.5 -37.5
##   .. .. .. ..$ lagrange   : num [1, 1] -3.24
##   .. .. .. ..$ hessian    : num [1:5, 1:5] 0.3338 0.0288 -7.6954 -1.1607 -0.7206 ...
##   .. .. .. ..$ ineqx0     : Named num 0.999
##   .. .. .. .. ..- attr(*, "names")= chr ""
##   .. .. .. ..$ nfuneval   : num 185
##   .. .. .. ..$ outer.iter : num 2
##   .. .. .. ..$ elapsed    : 'difftime' num 0.0850160121917725
##   .. .. .. .. ..- attr(*, "units")= chr "secs"
##   .. .. .. ..$ vscale     : num [1:6] 1 1 1 1 1 1
##   .. .. ..$ hess: NULL
##   ..@ model:List of 11
##   .. ..$ modelinc  : Named num [1:22] 1 0 0 0 0 0 1 1 1 0 ...
##   .. .. ..- attr(*, "names")= chr [1:22] "mu" "ar" "ma" "arfima" ...
##   .. ..$ modeldesc :List of 3
##   .. .. ..$ distribution: chr "norm"
##   .. .. ..$ distno      : int 1
##   .. .. ..$ vmodel      : chr "sGARCH"
##   .. ..$ modeldata :List of 4
##   .. .. ..$ T     : int 62
##   .. .. ..$ data  : num [1:62] 0.0467 -0.0504 -0.0482 -0.084 0.1508 ...
##   .. .. ..$ index : POSIXct[1:62], format: "1970-01-02 01:00:00" ...
##   .. .. ..$ period: 'difftime' num 1
##   .. .. .. ..- attr(*, "units")= chr "days"
##   .. ..$ pars      : num [1:19, 1:6] 0.00781 0 0 0 0 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:19] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:6] "Level" "Fixed" "Include" "Estimate" ...
##   .. ..$ start.pars: NULL
##   .. ..$ fixed.pars: NULL
##   .. ..$ maxOrder  : num 1
##   .. ..$ pos.matrix: num [1:21, 1:3] 1 0 0 0 0 0 2 3 4 0 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:21] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:3] "start" "stop" "include"
##   .. ..$ fmodel    : NULL
##   .. ..$ pidx      : num [1:19, 1:2] 1 2 3 4 5 6 7 8 9 10 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:19] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:2] "begin" "end"
##   .. ..$ n.start   : num 0
garch_11@fit$sigma
##  [1] 0.1329958 0.1333227 0.1336486 0.1339732 0.1342968 0.1346193 0.1349407
##  [8] 0.1352610 0.1355802 0.1358984 0.1362155 0.1365315 0.1368465 0.1371605
## [15] 0.1374734 0.1377854 0.1380962 0.1384061 0.1387150 0.1390229 0.1393298
## [22] 0.1396358 0.1399407 0.1402447 0.1405477 0.1408498 0.1411509 0.1414511
## [29] 0.1417503 0.1420487 0.1423460 0.1426425 0.1429381 0.1432328 0.1435265
## [36] 0.1438194 0.1441114 0.1444025 0.1446927 0.1449821 0.1452706 0.1455582
## [43] 0.1458450 0.1461309 0.1464160 0.1467002 0.1469836 0.1472662 0.1475480
## [50] 0.1478289 0.1481091 0.1483884 0.1486669 0.1489446 0.1492216 0.1494977
## [57] 0.1497731 0.1500476 0.1503214 0.1505945 0.1508667 0.1511382
portfel_probka_in$VaR <- q01 * garch_11@fit$sigma
tail(portfel_probka_in)
##           Date      zwroty  zwrotystd        VaR
## 116 2019-04-26 -0.02431955 -0.2332355 -0.3440883
## 117 2019-04-27 -0.01574112 -0.1692550 -0.3447191
## 118 2019-04-28 -0.06799414 -0.5589739 -0.3453481
## 119 2019-04-29 -0.16912923 -1.3132701 -0.3459754
## 120 2019-04-30  0.29343999  2.1367114 -0.3466009
## 121 2019-05-01 -0.06367050 -0.5267269 -0.3472246

Kwantyle nie równią się od siebie w znaczącym stopniu.

Wykres zwrotów i wartości Value-At_Risk przedstawia się w następujący sposób:

plot(portfel_probka_in$Date, portfel_probka_in$zwroty, col = "red", lwd = 1, type = 'l',
     ylim = c(-2,2))
abline(h = 0, lty = 2)
lines(portfel_probka_in$Date, portfel_probka_in$VaR, type = 'l', col = "green")

sum(portfel_probka_in$zwroty < portfel_probka_in$VaR) / length(portfel_probka_in$VaR)
## [1] 0.01612903

Straty przekroczyły zakładany poziom VaR w 1,6% przypadków.

Natomiast dla out-of-sample:

#1-dniowa prognoza warunkowego odchylenia standardowego
sigma.forecast1 <- ugarchforecast(garch_11, n.ahead = 1)
str(sigma.forecast1)
## Formal class 'uGARCHforecast' [package "rugarch"] with 2 slots
##   ..@ forecast:List of 6
##   .. ..$ n.ahead  : num 1
##   .. ..$ N        : num 62
##   .. ..$ n.start  : num 0
##   .. ..$ n.roll   : num 0
##   .. ..$ sigmaFor : num [1, 1] 0.151
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr "T+1"
##   .. .. .. ..$ : chr "1970-03-04 01:00:00"
##   .. ..$ seriesFor: num [1, 1] 0.00781
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr "T+1"
##   .. .. .. ..$ : chr "1970-03-04 01:00:00"
##   ..@ model   :List of 11
##   .. ..$ modelinc  : Named num [1:22] 1 0 0 0 0 0 1 1 1 0 ...
##   .. .. ..- attr(*, "names")= chr [1:22] "mu" "ar" "ma" "arfima" ...
##   .. ..$ modeldesc :List of 3
##   .. .. ..$ distribution: chr "norm"
##   .. .. ..$ distno      : int 1
##   .. .. ..$ vmodel      : chr "sGARCH"
##   .. ..$ modeldata :List of 6
##   .. .. ..$ T        : int 62
##   .. .. ..$ data     : num [1:62] 0.0467 -0.0504 -0.0482 -0.084 0.1508 ...
##   .. .. ..$ index    : POSIXct[1:62], format: "1970-01-02 01:00:00" ...
##   .. .. ..$ period   : 'difftime' num 1
##   .. .. .. ..- attr(*, "units")= chr "days"
##   .. .. ..$ sigma    : num [1:62] 0.133 0.133 0.134 0.134 0.134 ...
##   .. .. ..$ residuals: num [1:62] 0.0389 -0.0582 -0.056 -0.0918 0.143 ...
##   .. ..$ pars      : num [1:19, 1:6] 0.00781 0 0 0 0 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:19] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:6] "Level" "Fixed" "Include" "Estimate" ...
##   .. ..$ start.pars: NULL
##   .. ..$ fixed.pars: NULL
##   .. ..$ maxOrder  : num 1
##   .. ..$ pos.matrix: num [1:21, 1:3] 1 0 0 0 0 0 2 3 4 0 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:21] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:3] "start" "stop" "include"
##   .. ..$ fmodel    : NULL
##   .. ..$ pidx      : num [1:19, 1:2] 1 2 3 4 5 6 7 8 9 10 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:19] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:2] "begin" "end"
##   .. ..$ n.start   : num 0
str(sigma.forecast1@forecast)
## List of 6
##  $ n.ahead  : num 1
##  $ N        : num 62
##  $ n.start  : num 0
##  $ n.roll   : num 0
##  $ sigmaFor : num [1, 1] 0.151
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr "T+1"
##   .. ..$ : chr "1970-03-04 01:00:00"
##  $ seriesFor: num [1, 1] 0.00781
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr "T+1"
##   .. ..$ : chr "1970-03-04 01:00:00"
sigma.forecast1@forecast$sigmaFor
##     1970-03-04 01:00:00
## T+1            0.151409
sigma.forecast1_2 <- sigma.forecast1@forecast$sigmaFor[1, 1]

# 1-dniowy VaR:
q01 * sigma.forecast1_2
##         1% 
## -0.3478466
portfel$obs<-1:length(portfel$zwroty)
start  <- portfel$obs[portfel$Date == as.Date("2019-03-02")]
finish <- portfel$obs[portfel$Date == as.Date("2019-06-01")]
portfel1 <-portfel[start:finish, ]
VaR1 <- rep(NA, times = finish - start + 1)

time1 <- Sys.time()
for (k in start:finish) {
  tmp.data <- portfel[portfel$obs <= (k - 1), ]
  tmp.data <- tmp.data[as.Date("2019-01-02") <= tmp.data$Date, ]
  tmp.data$zwrotystd <- (tmp.data$zwroty - mean(tmp.data$zwroty, na.rm = T)) /
    sd(tmp.data$zwroty, na.rm = T)
  q01 <- quantile(tmp.data$zwrotystd, 0.01, na.rm = T)
  spec10 <- ugarchspec(variance.model = list(model = "sGARCH",
                                           garchOrder = c(1, 0)),
                     mean.model = list(armaOrder = c(0, 0),
                                       include.mean = T),
                     distribution.model = "norm")
  tmp.garch10 <- ugarchfit(spec = spec_10, data = na.omit(portfel$zwroty))
  sigma.forecast1  <- ugarchforecast(tmp.garch10, n.ahead = 1)
  sigma.forecast12 <- sigma.forecast1@forecast$sigmaFor[1, 1]
  VaR1[k - start + 1] <- q01 * sigma.forecast12
}
time2 <- Sys.time()

time2 - time1
## Time difference of 12.23209 secs
portfel1$VaR1 <- VaR1

plot(portfel1$Date, portfel1$zwroty, col = "red", lwd = 1, type = 'l',
     ylim = c(-1, 1))
abline(h = 0, lty = 2)
lines(portfel1$Date, portfel1$VaR1, type = 'l', col = "green")

sum(portfel1$zwroty < portfel1$VaR1) / length(portfel1$VaR1)
## [1] 0

Straty przekroczyły zakładany poziom VaR w 0% przypadków.

Szacowanie modelu tGARCH:

tspec_10 <- ugarchspec(variance.model = list(model = "fGARCH",
                                            garchOrder = c(1, 0),
                                            submodel = "TGARCH"),
                      mean.model = list(armaOrder = c(0, 0),
                                        include.mean = F),
                      distribution.model = "norm")

k.tgarch_10 <- ugarchfit(spec = tspec_10, data = na.omit(portfel_probka_in$zwroty))
## Warning in .fgarchfit(spec = spec, data = data, out.sample = out.sample, : 
## ugarchfit-->waring: using less than 100 data
##  points for estimation
tspec_11 <- ugarchspec(variance.model = list(model = "fGARCH",
                        garchOrder = c(1, 1),
                        submodel = "TGARCH"),
  mean.model = list(armaOrder = c(0, 0),
                    include.mean = F),
  distribution.model = "norm")

k.tgarch_11 <- ugarchfit(spec = tspec_11, data = na.omit(portfel_probka_in$zwroty))
## Warning in .fgarchfit(spec = spec, data = data, out.sample = out.sample, : 
## ugarchfit-->waring: using less than 100 data
##  points for estimation
tspec_21 <- ugarchspec(variance.model = list(model = "fGARCH",
                                            garchOrder = c(2, 1),
                                            submodel = "TGARCH"),
                      mean.model = list(armaOrder = c(0, 0),
                                        include.mean = F),
                      distribution.model = "norm")

k.tgarch_21 <- ugarchfit(spec = tspec_21, data = na.omit(portfel_probka_in$zwroty))
## Warning in .fgarchfit(spec = spec, data = data, out.sample = out.sample, : 
## ugarchfit-->waring: using less than 100 data
##  points for estimation
tspec_30 <- ugarchspec(variance.model = list(model = "fGARCH",
                                            garchOrder = c(3, 0),
                                            submodel = "TGARCH"),
                      mean.model = list(armaOrder = c(0, 0),
                                        include.mean = F),
                      distribution.model = "norm")

k.tgarch_30 <- ugarchfit(spec = tspec_30, data = na.omit(portfel_probka_in$zwroty))
## Warning in .fgarchfit(spec = spec, data = data, out.sample = out.sample, : 
## ugarchfit-->waring: using less than 100 data
##  points for estimation
k.tgarch_10
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : fGARCH(1,0)
## fGARCH Sub-Model : TGARCH
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error  t value Pr(>|t|)
## omega   0.000018    0.000000   302.46        0
## alpha1  0.137546    0.000454   302.88        0
## eta11   0.653886    0.002187   298.99        0
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## omega   0.000018    0.000143 0.128586  0.89769
## alpha1  0.137546    1.946785 0.070653  0.94367
## eta11   0.653886    0.914960 0.714660  0.47482
## 
## LogLikelihood : -3202.228 
## 
## Information Criteria
## ------------------------------------
##                    
## Akaike       103.39
## Bayes        103.50
## Shibata      103.39
## Hannan-Quinn 103.43
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                    0.05409  0.8161
## Lag[2*(p+q)+(p+q)-1][2]   0.06007  0.9486
## Lag[4*(p+q)+(p+q)-1][5]   0.12687  0.9970
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                    0.02870  0.8655
## Lag[2*(p+q)+(p+q)-1][2]   0.04130  0.9631
## Lag[4*(p+q)+(p+q)-1][5]   0.08264  0.9987
## d.o.f=1
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[2]   0.02362 0.500 2.000  0.8779
## ARCH Lag[4]   0.05440 1.397 1.611  0.9930
## ARCH Lag[6]   0.08574 2.222 1.500  0.9993
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  3.65
## Individual Statistics:             
## omega  3.1707
## alpha1 0.2741
## eta11  0.2741
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          0.846 1.01 1.35
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias            1.325 0.1903    
## Negative Sign Bias   0.143 0.8868    
## Positive Sign Bias   1.457 0.1505    
## Joint Effect         3.030 0.3870    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     412.8    9.336e-76
## 2    30     649.3   3.693e-118
## 3    40     885.1   2.136e-160
## 4    50    1083.2   9.005e-195
## 
## 
## Elapsed time : 0.2447441
k.tgarch_11
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : fGARCH(1,1)
## fGARCH Sub-Model : TGARCH
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error   t value Pr(>|t|)
## omega   0.002279    0.004699  0.485102   0.6276
## alpha1  0.000000    0.000002  0.000131   0.9999
## beta1   0.991708    0.038038 26.071406   0.0000
## eta11  -0.985951    1.677084 -0.587896   0.5566
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## omega   0.002279    0.004043  0.56384  0.57286
## alpha1  0.000000    0.000000  0.30145  0.76307
## beta1   0.991708    0.029365 33.77200  0.00000
## eta11  -0.985951    4.316444 -0.22842  0.81932
## 
## LogLikelihood : 39.61836 
## 
## Information Criteria
## ------------------------------------
##                     
## Akaike       -1.1490
## Bayes        -1.0117
## Shibata      -1.1567
## Hannan-Quinn -1.0951
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      1.806  0.1790
## Lag[2*(p+q)+(p+q)-1][2]     1.865  0.2862
## Lag[4*(p+q)+(p+q)-1][5]     2.986  0.4097
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                    0.06796  0.7943
## Lag[2*(p+q)+(p+q)-1][5]   0.86983  0.8884
## Lag[4*(p+q)+(p+q)-1][9]   2.68274  0.8100
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3]   0.08566 0.500 2.000  0.7698
## ARCH Lag[5]   0.70240 1.440 1.667  0.8227
## ARCH Lag[7]   1.50849 2.315 1.543  0.8197
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  0.2742
## Individual Statistics:              
## omega  0.05682
## alpha1 0.05023
## beta1  0.05659
## eta11  0.06104
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.07 1.24 1.6
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias           0.4090 0.6841    
## Negative Sign Bias  0.1023 0.9189    
## Positive Sign Bias  0.3188 0.7511    
## Joint Effect        0.1904 0.9791    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     19.29       0.4384
## 2    30     23.16       0.7691
## 3    40     33.48       0.7191
## 4    50     34.77       0.9378
## 
## 
## Elapsed time : 0.1547849
k.tgarch_21
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : fGARCH(2,1)
## fGARCH Sub-Model : TGARCH
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error   t value Pr(>|t|)
## omega   0.002704    0.005327  0.507608  0.61173
## alpha1  0.000000    0.000004  0.000823  0.99934
## alpha2  0.000000    0.000002  0.003638  0.99710
## beta1   0.988632    0.042721 23.141649  0.00000
## eta11   0.438115    0.571997  0.765940  0.44371
## eta12   0.999697    6.248077  0.160001  0.87288
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## omega   0.002704    0.004923  0.54930  0.58280
## alpha1  0.000000    0.000000  0.40615  0.68463
## alpha2  0.000000    0.000000  0.36311  0.71652
## beta1   0.988632    0.035885 27.55031  0.00000
## eta11   0.438115    1.646657  0.26606  0.79019
## eta12   0.999697    3.530842  0.28313  0.77707
## 
## LogLikelihood : 39.67585 
## 
## Information Criteria
## ------------------------------------
##                      
## Akaike       -1.08632
## Bayes        -0.88047
## Shibata      -1.10294
## Hannan-Quinn -1.00550
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      1.829  0.1762
## Lag[2*(p+q)+(p+q)-1][2]     1.889  0.2818
## Lag[4*(p+q)+(p+q)-1][5]     3.014  0.4045
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                          statistic p-value
## Lag[1]                     0.06975  0.7917
## Lag[2*(p+q)+(p+q)-1][8]    1.79190  0.8896
## Lag[4*(p+q)+(p+q)-1][14]   5.50283  0.7051
## d.o.f=3
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[4]    0.3762 0.500 2.000  0.5396
## ARCH Lag[6]    1.3180 1.461 1.711  0.6588
## ARCH Lag[8]    2.0300 2.368 1.583  0.7355
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  0.2874
## Individual Statistics:              
## omega  0.05579
## alpha1 0.05307
## alpha2 0.05399
## beta1  0.05563
## eta11  0.05946
## eta12  0.05648
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.49 1.68 2.12
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias           0.4061 0.6862    
## Negative Sign Bias  0.1079 0.9144    
## Positive Sign Bias  0.3221 0.7486    
## Joint Effect        0.1879 0.9795    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     17.35       0.5658
## 2    30     20.26       0.8847
## 3    40     29.61       0.8611
## 4    50     33.16       0.9595
## 
## 
## Elapsed time : 0.3316138
k.tgarch_30
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : fGARCH(3,0)
## fGARCH Sub-Model : TGARCH
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error   t value Pr(>|t|)
## omega   0.095976    0.004304  22.30122  0.00000
## alpha1  0.098090    0.111408   0.88045  0.37861
## alpha2  0.000000    0.000001   0.00000  1.00000
## alpha3  1.000000    0.074849  13.36019  0.00000
## eta11   0.972203    0.905793   1.07332  0.28313
## eta12   1.000000    1.129841   0.88508  0.37611
## eta13  -0.798866    0.040515 -19.71763  0.00000
## 
## Robust Standard Errors:
##         Estimate  Std. Error    t value Pr(>|t|)
## omega   0.095976    0.006907  13.895990 0.000000
## alpha1  0.098090    0.145291   0.675124 0.499597
## alpha2  0.000000    0.000000   0.000308 0.999754
## alpha3  1.000000    0.366142   2.731182 0.006311
## eta11   0.972203    1.448094   0.671367 0.501986
## eta12   1.000000    3.706532   0.269794 0.787319
## eta13  -0.798866    0.062346 -12.813347 0.000000
## 
## LogLikelihood : 36.86164 
## 
## Information Criteria
## ------------------------------------
##                      
## Akaike       -0.96328
## Bayes        -0.72312
## Shibata      -0.98549
## Hannan-Quinn -0.86899
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                     0.9146  0.3389
## Lag[2*(p+q)+(p+q)-1][2]    0.9816  0.5043
## Lag[4*(p+q)+(p+q)-1][5]    2.4036  0.5266
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                          statistic p-value
## Lag[1]                    0.003778  0.9510
## Lag[2*(p+q)+(p+q)-1][8]   1.195486  0.9587
## Lag[4*(p+q)+(p+q)-1][14]  6.768152  0.5360
## d.o.f=3
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[4]    0.0427 0.500 2.000  0.8363
## ARCH Lag[6]    0.3540 1.461 1.711  0.9315
## ARCH Lag[8]    0.8131 2.368 1.583  0.9498
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  0.2707
## Individual Statistics:              
## omega  0.10141
## alpha1 0.07344
## alpha2 0.36251
## alpha3 0.08342
## eta11  0.12555
## eta12      NaN
## eta13  0.06335
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.69 1.9 2.35
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias          0.01261 0.9900    
## Negative Sign Bias 0.17346 0.8629    
## Positive Sign Bias 0.04569 0.9637    
## Joint Effect       0.06357 0.9958    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     19.94       0.3985
## 2    30     26.06       0.6220
## 3    40     32.19       0.7716
## 4    50     55.74       0.2361
## 
## 
## Elapsed time : 1.08135

k.tgarch_10 Akaike 103.39 Bayes 103.50 Shibata 103.39 Hannan-Quinn 103.43

k.tgarch_11 Akaike -1.1490 Bayes -1.0117 Shibata -1.1567 Hannan-Quinn -1.0951

k.tgarch_21 Akaike -1.08632 Bayes -0.88047 Shibata -1.10294 Hannan-Quinn -1.00550

k.tgarch_30 Akaike -0.96328 Bayes -0.72312 Shibata -0.98549 Hannan-Quinn -0.86899

Na podstawie kryteriów informacyjnych wybieramy model tGARCH(1,1).

Wykres funkcji autokorelacji dla tGARCH(1,1):

plot(k.tgarch_11, which = 10)

Wypustki są nieistotne, ponieważ nie przekraczają obszaru krytycznego. Zatem nie występuje autokorelacja reszt.

Kolejnym krokiem jest obliczenie Value-At_Risk w in sample i out of sample oraz porównanie kwantyli empirycznych ze standardowym z rozkładu normalnego.

portfel_probka_in$zwrotystd2 <- (portfel_probka_in$zwroty - mean(portfel_probka_in$zwroty, na.rm=T)) /
  sd(portfel_probka_in$zwroty ,na.rm = T)
tail(portfel_probka_in)
##           Date      zwroty  zwrotystd        VaR zwrotystd2
## 116 2019-04-26 -0.02431955 -0.2332355 -0.3440883 -0.2332355
## 117 2019-04-27 -0.01574112 -0.1692550 -0.3447191 -0.1692550
## 118 2019-04-28 -0.06799414 -0.5589739 -0.3453481 -0.5589739
## 119 2019-04-29 -0.16912923 -1.3132701 -0.3459754 -1.3132701
## 120 2019-04-30  0.29343999  2.1367114 -0.3466009  2.1367114
## 121 2019-05-01 -0.06367050 -0.5267269 -0.3472246 -0.5267269
basicStats(portfel_probka_in$zwrotystd2)
##             X..portfel_probka_in.zwrotystd2
## nobs                              62.000000
## NAs                                0.000000
## Minimum                           -2.831300
## Maximum                            3.986365
## 1. Quartile                       -0.510065
## 3. Quartile                        0.520829
## Mean                               0.000000
## Median                            -0.059323
## Sum                                0.000000
## SE Mean                            0.127000
## LCL Mean                          -0.253952
## UCL Mean                           0.253952
## Variance                           1.000000
## Stdev                              1.000000
## Skewness                           0.632851
## Kurtosis                           3.294645
q01 <- quantile(portfel_probka_in$zwrotystd2, 0.01, na.rm = T)
q01
##        1% 
## -2.297398
qnorm(0.01, 0, 1)
## [1] -2.326348
str(k.tgarch_11)
## Formal class 'uGARCHfit' [package "rugarch"] with 2 slots
##   ..@ fit  :List of 27
##   .. ..$ hessian        : num [1:4, 1:4] 4.99e+06 4.90e+05 6.14e+05 -2.38e-05 4.90e+05 ...
##   .. ..$ cvar           : num [1:4, 1:4] 2.21e-05 -8.12e-13 -1.78e-04 6.68e-03 -8.12e-13 ...
##   .. ..$ var            : num [1:62] 0.00904 0.00932 0.00961 0.0099 0.01019 ...
##   .. ..$ sigma          : num [1:62] 0.0951 0.0966 0.098 0.0995 0.101 ...
##   .. ..$ condH          : num NaN
##   .. ..$ z              : num [1:62] 0.491 -0.522 -0.492 -0.844 1.493 ...
##   .. ..$ LLH            : num 39.6
##   .. ..$ log.likelihoods: num [1:62] -1.314 -1.282 -1.282 -1.032 -0.259 ...
##   .. ..$ residuals      : num [1:62] 0.0467 -0.0504 -0.0482 -0.084 0.1508 ...
##   .. ..$ coef           : Named num [1:4] 2.28e-03 2.80e-10 9.92e-01 -9.86e-01
##   .. .. ..- attr(*, "names")= chr [1:4] "omega" "alpha1" "beta1" "eta11"
##   .. ..$ robust.cvar    : num [1:4, 1:4] 1.63e-05 3.18e-14 -1.18e-04 -6.03e-03 3.18e-14 ...
##   .. ..$ A              : num [1:4, 1:4] 8.06e+04 7.90e+03 9.91e+03 -3.84e-07 7.90e+03 ...
##   .. ..$ B              : num [1:4, 1:4] 1.62e+05 1.49e+04 1.92e+04 -7.66e-07 1.49e+04 ...
##   .. ..$ scores         : num [1:62, 1:4] 0 7.53 15.4 8.58 -48.13 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : NULL
##   .. .. .. ..$ : chr [1:4] "omega" "alpha1" "beta1" "eta11"
##   .. ..$ se.coef        : num [1:4] 4.70e-03 2.14e-06 3.80e-02 1.68
##   .. ..$ tval           : Named num [1:4] 0.485102 0.000131 26.071406 -0.587896
##   .. .. ..- attr(*, "names")= chr [1:4] "omega" "alpha1" "beta1" "eta11"
##   .. ..$ matcoef        : num [1:4, 1:4] 2.28e-03 2.80e-10 9.92e-01 -9.86e-01 4.70e-03 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:4] "omega" "alpha1" "beta1" "eta11"
##   .. .. .. ..$ : chr [1:4] " Estimate" " Std. Error" " t value" "Pr(>|t|)"
##   .. ..$ robust.se.coef : num [1:4] 4.04e-03 9.30e-10 2.94e-02 4.32
##   .. ..$ robust.tval    : Named num [1:4] 0.564 0.301 33.772 -0.228
##   .. .. ..- attr(*, "names")= chr [1:4] "omega" "alpha1" "beta1" "eta11"
##   .. ..$ robust.matcoef : num [1:4, 1:4] 2.28e-03 2.80e-10 9.92e-01 -9.86e-01 4.04e-03 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:4] "omega" "alpha1" "beta1" "eta11"
##   .. .. .. ..$ : chr [1:4] " Estimate" " Std. Error" " t value" "Pr(>|t|)"
##   .. ..$ fitted.values  : num [1:62] 0 0 0 0 0 0 0 0 0 0 ...
##   .. ..$ convergence    : num 0
##   .. ..$ kappa          : num 0.798
##   .. ..$ persistence    : num 0.992
##   .. ..$ timer          : 'difftime' num 0.154784917831421
##   .. .. ..- attr(*, "units")= chr "secs"
##   .. ..$ ipars          : num [1:19, 1:6] 0 0 0 0 0 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:19] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:6] "Level" "Fixed" "Include" "Estimate" ...
##   .. ..$ solver         :List of 2
##   .. .. ..$ sol :List of 10
##   .. .. .. ..$ pars       : Named num [1:4] 2.28e-03 2.80e-10 9.92e-01 -9.86e-01
##   .. .. .. .. ..- attr(*, "names")= chr [1:4] "omega" "alpha1" "beta1" "eta11"
##   .. .. .. ..$ convergence: num 0
##   .. .. .. ..$ values     : num [1:3] 148.2 -39.6 -39.6
##   .. .. .. ..$ lagrange   : num [1, 1] -5.4e-07
##   .. .. .. ..$ hessian    : num [1:5, 1:5] 0.461 -73.11 -7.171 -8.486 0.134 ...
##   .. .. .. ..$ ineqx0     : Named num 0.992
##   .. .. .. .. ..- attr(*, "names")= chr ""
##   .. .. .. ..$ nfuneval   : num 166
##   .. .. .. ..$ outer.iter : num 2
##   .. .. .. ..$ elapsed    : 'difftime' num 0.152949094772339
##   .. .. .. .. ..- attr(*, "units")= chr "secs"
##   .. .. .. ..$ vscale     : num [1:6] 1 1 1 1 1 1
##   .. .. ..$ hess: NULL
##   ..@ model:List of 11
##   .. ..$ modelinc  : Named num [1:22] 0 0 0 0 0 0 1 1 1 0 ...
##   .. .. ..- attr(*, "names")= chr [1:22] "mu" "ar" "ma" "arfima" ...
##   .. ..$ modeldesc :List of 4
##   .. .. ..$ distribution: chr "norm"
##   .. .. ..$ distno      : int 1
##   .. .. ..$ vmodel      : chr "fGARCH"
##   .. .. ..$ vsubmodel   : chr "TGARCH"
##   .. ..$ modeldata :List of 4
##   .. .. ..$ T     : int 62
##   .. .. ..$ data  : num [1:62] 0.0467 -0.0504 -0.0482 -0.084 0.1508 ...
##   .. .. ..$ index : POSIXct[1:62], format: "1970-01-02 01:00:00" ...
##   .. .. ..$ period: 'difftime' num 1
##   .. .. .. ..- attr(*, "units")= chr "days"
##   .. ..$ pars      : num [1:19, 1:6] 0 0 0 0 0 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:19] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:6] "Level" "Fixed" "Include" "Estimate" ...
##   .. ..$ start.pars: NULL
##   .. ..$ fixed.pars: NULL
##   .. ..$ maxOrder  : num 1
##   .. ..$ pos.matrix: num [1:21, 1:3] 0 0 0 0 0 0 1 2 3 0 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:21] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:3] "start" "stop" "include"
##   .. ..$ fmodel    :List of 4
##   .. .. ..$ fpars     :List of 5
##   .. .. .. ..$ lambda: num 1
##   .. .. .. ..$ delta : num 1
##   .. .. .. ..$ eta1  : num 0.05
##   .. .. .. ..$ eta2  : num 0
##   .. .. .. ..$ fk    : num 0
##   .. .. ..$ finclude  : num [1:5] 0 0 1 0 0
##   .. .. ..$ fgarchtype: num 2
##   .. .. ..$ fbounds   :List of 2
##   .. .. .. ..$ LB: num [1:5] 1 1 -1 0 0
##   .. .. .. ..$ UB: num [1:5] 1 1 1 0 0
##   .. ..$ pidx      : num [1:19, 1:2] 1 2 3 4 5 6 7 8 9 10 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:19] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:2] "begin" "end"
##   .. ..$ n.start   : num 0
k.tgarch_11@fit$sigma
##  [1] 0.09506790 0.09655897 0.09803768 0.09950412 0.10095841 0.10240063
##  [7] 0.10383090 0.10524931 0.10665596 0.10805094 0.10943436 0.11080631
## [13] 0.11216688 0.11351617 0.11485427 0.11618128 0.11749729 0.11880238
## [19] 0.12009665 0.12138019 0.12265309 0.12391543 0.12516731 0.12640880
## [25] 0.12764001 0.12886100 0.13007187 0.13127270 0.13246357 0.13364457
## [31] 0.13481578 0.13597727 0.13712913 0.13827145 0.13940429 0.14052774
## [37] 0.14164187 0.14274677 0.14384250 0.14492915 0.14600679 0.14707549
## [43] 0.14813534 0.14918639 0.15022873 0.15126243 0.15228755 0.15330418
## [49] 0.15431237 0.15531221 0.15630376 0.15728708 0.15826226 0.15922934
## [55] 0.16018841 0.16113952 0.16208275 0.16301816 0.16394581 0.16486577
## [61] 0.16577811 0.16668287
portfel_probka_in$VaR2 <- q01 * k.tgarch_11@fit$sigma
tail(portfel_probka_in)
##           Date      zwroty  zwrotystd        VaR zwrotystd2       VaR2
## 116 2019-04-26 -0.02431955 -0.2332355 -0.3440883 -0.2332355 -0.3723686
## 117 2019-04-27 -0.01574112 -0.1692550 -0.3447191 -0.1692550 -0.3745176
## 118 2019-04-28 -0.06799414 -0.5589739 -0.3453481 -0.5589739 -0.3766487
## 119 2019-04-29 -0.16912923 -1.3132701 -0.3459754 -1.3132701 -0.3787623
## 120 2019-04-30  0.29343999  2.1367114 -0.3466009  2.1367114 -0.3808582
## 121 2019-05-01 -0.06367050 -0.5267269 -0.3472246 -0.5267269 -0.3829369

Wykres zwrotów i VaR.

plot(portfel_probka_in$Date, portfel_probka_in$zwroty, col = "red", lwd = 1, type = 'l',
     ylim = c(-2,2))
abline(h = 0, lty = 2)
lines(portfel_probka_in$Date, portfel_probka_in$VaR2, type = 'l', col = "green")

sum(portfel_probka_in$zwroty < portfel_probka_in$VaR2) / length(portfel_probka_in$VaR2)
## [1] 0.01612903

W 1,6% przypadkach straty przekroczyły zakładany poziom VaR.

Natomiast dla out of sample:

sigma.forecast2 <- ugarchforecast(k.tgarch_11, n.ahead = 1)

str(sigma.forecast2)
## Formal class 'uGARCHforecast' [package "rugarch"] with 2 slots
##   ..@ forecast:List of 6
##   .. ..$ n.ahead  : num 1
##   .. ..$ N        : num 62
##   .. ..$ n.start  : num 0
##   .. ..$ n.roll   : num 0
##   .. ..$ sigmaFor : num [1, 1] 0.168
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr "T+1"
##   .. .. .. ..$ : chr "1970-03-04 01:00:00"
##   .. ..$ seriesFor: num [1, 1] 0
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr "T+1"
##   .. .. .. ..$ : chr "1970-03-04 01:00:00"
##   ..@ model   :List of 11
##   .. ..$ modelinc  : Named num [1:22] 0 0 0 0 0 0 1 1 1 0 ...
##   .. .. ..- attr(*, "names")= chr [1:22] "mu" "ar" "ma" "arfima" ...
##   .. ..$ modeldesc :List of 4
##   .. .. ..$ distribution: chr "norm"
##   .. .. ..$ distno      : int 1
##   .. .. ..$ vmodel      : chr "fGARCH"
##   .. .. ..$ vsubmodel   : chr "TGARCH"
##   .. ..$ modeldata :List of 6
##   .. .. ..$ T        : int 62
##   .. .. ..$ data     : num [1:62] 0.0467 -0.0504 -0.0482 -0.084 0.1508 ...
##   .. .. ..$ index    : POSIXct[1:62], format: "1970-01-02 01:00:00" ...
##   .. .. ..$ period   : 'difftime' num 1
##   .. .. .. ..- attr(*, "units")= chr "days"
##   .. .. ..$ sigma    : num [1:62] 0.0951 0.0966 0.098 0.0995 0.101 ...
##   .. .. ..$ residuals: num [1:62] 0.0467 -0.0504 -0.0482 -0.084 0.1508 ...
##   .. ..$ pars      : num [1:19, 1:6] 0 0 0 0 0 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:19] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:6] "Level" "Fixed" "Include" "Estimate" ...
##   .. ..$ start.pars: NULL
##   .. ..$ fixed.pars: NULL
##   .. ..$ maxOrder  : num 1
##   .. ..$ pos.matrix: num [1:21, 1:3] 0 0 0 0 0 0 1 2 3 0 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:21] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:3] "start" "stop" "include"
##   .. ..$ fmodel    :List of 4
##   .. .. ..$ fpars     :List of 5
##   .. .. .. ..$ lambda: num 1
##   .. .. .. ..$ delta : num 1
##   .. .. .. ..$ eta1  : num 0.05
##   .. .. .. ..$ eta2  : num 0
##   .. .. .. ..$ fk    : num 0
##   .. .. ..$ finclude  : num [1:5] 0 0 1 0 0
##   .. .. ..$ fgarchtype: num 2
##   .. .. ..$ fbounds   :List of 2
##   .. .. .. ..$ LB: num [1:5] 1 1 -1 0 0
##   .. .. .. ..$ UB: num [1:5] 1 1 1 0 0
##   .. ..$ pidx      : num [1:19, 1:2] 1 2 3 4 5 6 7 8 9 10 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:19] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:2] "begin" "end"
##   .. ..$ n.start   : num 0
str(sigma.forecast2@forecast)
## List of 6
##  $ n.ahead  : num 1
##  $ N        : num 62
##  $ n.start  : num 0
##  $ n.roll   : num 0
##  $ sigmaFor : num [1, 1] 0.168
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr "T+1"
##   .. ..$ : chr "1970-03-04 01:00:00"
##  $ seriesFor: num [1, 1] 0
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr "T+1"
##   .. ..$ : chr "1970-03-04 01:00:00"
sigma.forecast2@forecast$sigmaFor
##     1970-03-04 01:00:00
## T+1           0.1675801
sigma.forecast22 <- sigma.forecast2@forecast$sigmaFor[1, 1]

# 1-dniowy VaR
q01 * sigma.forecast22
##         1% 
## -0.3849982
portfel$obs<-1:length(portfel$zwroty)
start  <- portfel$obs[portfel$Date == as.Date("2019-03-02")]
finish <- portfel$obs[portfel$Date == as.Date("2019-06-01")]
portfel4 <-portfel[start:finish, ]
VaR <- rep(NA, times = finish - start + 1)

time1 <- Sys.time()
for (k in start:finish) {
  tmp.data <- portfel[portfel$obs <= (k - 1), ]
  tmp.data <- tmp.data[as.Date("2019-01-02") <= tmp.data$Date, ]
  tmp.data$zwrotystd <- (tmp.data$zwroty - mean(tmp.data$zwroty, na.rm = T)) /
    sd(tmp.data$zwroty, na.rm = T)
  q01 <- quantile(tmp.data$zwrotystd, 0.01, na.rm = T)
  tspec11 <- ugarchspec(variance.model = list(model = "fGARCH",
                                              garchOrder = c(2, 1),
                                              submodel = "TGARCH"),
                        mean.model = list(armaOrder = c(0, 0),
                                          include.mean = F),
                        distribution.model = "norm")
  tmp.k.tgarch11 <- ugarchfit(spec = tspec_11, data = na.omit(portfel$zwroty))
  sigma.forecast2  <- ugarchforecast(tmp.k.tgarch11, n.ahead = 1)
  sigma.forecast22 <- sigma.forecast2@forecast$sigmaFor[1, 1]
  VaR[k - start + 1] <- q01 * sigma.forecast22
}
time2 <- Sys.time()

time2 - time1
## Time difference of 21.06684 secs
portfel4$VaR <- VaR

plot(portfel4$Date, portfel4$zwroty, col = "red", lwd = 1, type = 'l',
     ylim = c(-1, 1))
abline(h = 0, lty = 2)
lines(portfel4$Date, portfel4$VaR, type = 'l', col = "green")

Straty przekroczyły zakładany poziom VaR w 0% przypadków.

Prognozy warunkowej wariancji są do siebie zbliżone.

Kolejnym krokiem było oszacowanie modelu eGARCH.

espec_11 = ugarchspec(variance.model = list(model ="eGARCH",
                                        garchOrder = c(1, 1)),
                  mean.model = list(armaOrder = c(0, 0), include.mean = F),
                  distribution.model = "norm")

egarch_11 <- ugarchfit(spec = espec_11, data = na.omit(portfel_probka_in$zwroty))
## Warning in .egarchfit(spec = spec, data = data, out.sample = out.sample, : 
## ugarchfit-->waring: using less than 100 data
##  points for estimation
espec_12 = ugarchspec(variance.model = list(model ="eGARCH",
                                           garchOrder = c(1, 2)),
                     mean.model = list(armaOrder = c(0, 0), include.mean = F),
                     distribution.model = "norm")

egarch_12 <- ugarchfit(spec = espec_12, data = na.omit(portfel_probka_in$zwroty))
## Warning in .egarchfit(spec = spec, data = data, out.sample = out.sample, : 
## ugarchfit-->waring: using less than 100 data
##  points for estimation
espec_21 = ugarchspec(variance.model = list(model ="eGARCH",
                                           garchOrder = c(2, 1)),
                     mean.model = list(armaOrder = c(0, 0), include.mean = F),
                     distribution.model = "norm")

egarch_21 <- ugarchfit(spec = espec_21, data = na.omit(portfel_probka_in$zwroty))
## Warning in .egarchfit(spec = spec, data = data, out.sample = out.sample, : 
## ugarchfit-->waring: using less than 100 data
##  points for estimation
espec_13 = ugarchspec(variance.model = list(model ="eGARCH",
                                           garchOrder = c(1, 3)),
                     mean.model = list(armaOrder = c(0, 0), include.mean = F),
                     distribution.model = "norm")

egarch_13 <- ugarchfit(spec = espec_13, data = na.omit(portfel_probka_in$zwroty))
## Warning in .egarchfit(spec = spec, data = data, out.sample = out.sample, : 
## ugarchfit-->waring: using less than 100 data
##  points for estimation
espec_31 = ugarchspec(variance.model = list(model ="eGARCH",
                                           garchOrder = c(3, 1)),
                     mean.model = list(armaOrder = c(0, 0), include.mean = F),
                     distribution.model = "norm")

egarch_31 <- ugarchfit(spec = espec_31, data = na.omit(portfel_probka_in$zwroty))
## Warning in .egarchfit(spec = spec, data = data, out.sample = out.sample, : 
## ugarchfit-->waring: using less than 100 data
##  points for estimation
egarch_11
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : eGARCH(1,1)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error    t value Pr(>|t|)
## omega  -0.202778    0.000014 -14626.742        0
## alpha1  0.011072    0.000155     71.452        0
## beta1   0.989998    0.000264   3743.420        0
## gamma1 -0.604025    0.000245  -2463.039        0
## 
## Robust Standard Errors:
##         Estimate  Std. Error    t value Pr(>|t|)
## omega  -0.202778    0.000032 -6401.9881 0.000000
## alpha1  0.011072    0.004157     2.6633 0.007738
## beta1   0.989998    0.006251   158.3690 0.000000
## gamma1 -0.604025    0.001387  -435.4009 0.000000
## 
## LogLikelihood : 39.83726 
## 
## Information Criteria
## ------------------------------------
##                     
## Akaike       -1.1560
## Bayes        -1.0188
## Shibata      -1.1637
## Hannan-Quinn -1.1022
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      4.157 0.04146
## Lag[2*(p+q)+(p+q)-1][2]     4.295 0.06283
## Lag[4*(p+q)+(p+q)-1][5]     5.763 0.10224
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                    0.03917  0.8431
## Lag[2*(p+q)+(p+q)-1][5]   1.31294  0.7857
## Lag[4*(p+q)+(p+q)-1][9]   3.24404  0.7181
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3] 0.0005806 0.500 2.000  0.9808
## ARCH Lag[5] 0.2954018 1.440 1.667  0.9415
## ARCH Lag[7] 1.5187698 2.315 1.543  0.8176
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  2.7621
## Individual Statistics:              
## omega  0.08437
## alpha1 0.08358
## beta1  0.01735
## gamma1 0.08428
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.07 1.24 1.6
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias           0.3534 0.7251    
## Negative Sign Bias  1.3397 0.1856    
## Positive Sign Bias  0.3246 0.7466    
## Joint Effect        2.3919 0.4951    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     27.03       0.1039
## 2    30     27.03       0.5700
## 3    40     37.35       0.5450
## 4    50     51.28       0.3845
## 
## 
## Elapsed time : 0.200274
egarch_12
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : eGARCH(1,2)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error  t value Pr(>|t|)
## omega  -0.460382    0.000057  -8078.5        0
## alpha1  0.359605    0.000116   3112.3        0
## beta1   0.998011    0.000189   5273.1        0
## beta2  -0.063646    0.000013  -4890.2        0
## gamma1 -0.840378    0.000078 -10843.4        0
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## omega  -0.460382    0.004600 -100.077        0
## alpha1  0.359605    0.005514   65.223        0
## beta1   0.998011    0.013682   72.942        0
## beta2  -0.063646    0.000532 -119.545        0
## gamma1 -0.840378    0.008279 -101.502        0
## 
## LogLikelihood : 42.884 
## 
## Information Criteria
## ------------------------------------
##                     
## Akaike       -1.2221
## Bayes        -1.0505
## Shibata      -1.2338
## Hannan-Quinn -1.1547
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      6.107 0.01346
## Lag[2*(p+q)+(p+q)-1][2]     6.404 0.01717
## Lag[4*(p+q)+(p+q)-1][5]     7.505 0.03905
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                          statistic p-value
## Lag[1]                     0.08606  0.7692
## Lag[2*(p+q)+(p+q)-1][8]    1.45590  0.9323
## Lag[4*(p+q)+(p+q)-1][14]   3.48831  0.9239
## d.o.f=3
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[4]    0.8578 0.500 2.000  0.3544
## ARCH Lag[6]    1.5484 1.461 1.711  0.5982
## ARCH Lag[8]    2.4567 2.368 1.583  0.6492
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  7.4094
## Individual Statistics:              
## omega  0.04653
## alpha1 0.04697
## beta1  0.02107
## beta2  0.04657
## gamma1 0.04634
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.28 1.47 1.88
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias           0.1410 0.8884    
## Negative Sign Bias  0.9332 0.3547    
## Positive Sign Bias  0.2354 0.8148    
## Joint Effect        2.0300 0.5662    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     25.10      0.15738
## 2    30     43.48      0.04105
## 3    40     38.65      0.48591
## 4    50     44.45      0.65783
## 
## 
## Elapsed time : 0.5517762
egarch_21
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : eGARCH(2,1)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error   t value Pr(>|t|)
## omega  -1.743275    0.149016 -11.69854  0.00000
## alpha1 -0.094974    0.106868  -0.88870  0.37416
## alpha2  0.304436    0.195449   1.55763  0.11932
## beta1   0.587594    0.038619  15.21498  0.00000
## gamma1 -0.044683    0.338391  -0.13205  0.89495
## gamma2 -0.475254    0.309022  -1.53793  0.12406
## 
## Robust Standard Errors:
##         Estimate  Std. Error   t value Pr(>|t|)
## omega  -1.743275    0.156695 -11.12528 0.000000
## alpha1 -0.094974    0.124402  -0.76345 0.445197
## alpha2  0.304436    0.144555   2.10603 0.035202
## beta1   0.587594    0.036735  15.99553 0.000000
## gamma1 -0.044683    0.316167  -0.14133 0.887611
## gamma2 -0.475254    0.346831  -1.37027 0.170601
## 
## LogLikelihood : 39.6991 
## 
## Information Criteria
## ------------------------------------
##                      
## Akaike       -1.08707
## Bayes        -0.88122
## Shibata      -1.10369
## Hannan-Quinn -1.00625
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      2.681  0.1015
## Lag[2*(p+q)+(p+q)-1][2]     2.922  0.1473
## Lag[4*(p+q)+(p+q)-1][5]     4.357  0.2128
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                          statistic p-value
## Lag[1]                     0.04901  0.8248
## Lag[2*(p+q)+(p+q)-1][8]    3.44019  0.6049
## Lag[4*(p+q)+(p+q)-1][14]   8.76609  0.3033
## d.o.f=3
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[4]   0.01449 0.500 2.000  0.9042
## ARCH Lag[6]   2.54778 1.461 1.711  0.3816
## ARCH Lag[8]   3.62938 2.368 1.583  0.4330
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  0.9706
## Individual Statistics:              
## omega  0.23381
## alpha1 0.10071
## alpha2 0.03783
## beta1  0.22477
## gamma1 0.40247
## gamma2 0.38356
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.49 1.68 2.12
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias           0.6606 0.5115    
## Negative Sign Bias  0.2729 0.7859    
## Positive Sign Bias  0.4634 0.6449    
## Joint Effect        0.4586 0.9279    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     14.13       0.7761
## 2    30     18.32       0.9374
## 3    40     28.32       0.8968
## 4    50     39.61       0.8285
## 
## 
## Elapsed time : 0.1773648
egarch_13
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : eGARCH(1,3)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error  t value Pr(>|t|)
## omega   -0.49665    0.000053  -9449.9        0
## alpha1  -0.60972    0.000325  -1873.8        0
## beta1    0.26571    0.000059   4470.1        0
## beta2   -0.17054    0.000041  -4207.3        0
## beta3    0.86601    0.000175   4946.9        0
## gamma1  -1.53117    0.000342  -4477.4        0
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## omega   -0.49665    0.003222  -154.14        0
## alpha1  -0.60972    0.002817  -216.41        0
## beta1    0.26571    0.000828   321.02        0
## beta2   -0.17054    0.000703  -242.54        0
## beta3    0.86601    0.004238   204.36        0
## gamma1  -1.53117    0.008821  -173.59        0
## 
## LogLikelihood : 44.82932 
## 
## Information Criteria
## ------------------------------------
##                     
## Akaike       -1.2526
## Bayes        -1.0467
## Shibata      -1.2692
## Hannan-Quinn -1.1717
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic  p-value
## Lag[1]                      8.083 0.004469
## Lag[2*(p+q)+(p+q)-1][2]     8.295 0.005403
## Lag[4*(p+q)+(p+q)-1][5]     9.181 0.014896
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                          statistic  p-value
## Lag[1]                       7.928 0.004868
## Lag[2*(p+q)+(p+q)-1][11]    11.131 0.056892
## Lag[4*(p+q)+(p+q)-1][19]    13.093 0.188452
## d.o.f=4
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[5]   0.09174 0.500 2.000  0.7620
## ARCH Lag[7]   1.02962 1.473 1.746  0.7490
## ARCH Lag[9]   1.44525 2.402 1.619  0.8616
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  5.7275
## Individual Statistics:              
## omega  0.10584
## alpha1 0.10768
## beta1  0.03116
## beta2  0.10749
## beta3  0.01073
## gamma1 0.10628
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.49 1.68 2.12
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value      prob sig
## Sign Bias           0.9695 3.364e-01    
## Negative Sign Bias  2.1519 3.565e-02  **
## Positive Sign Bias  4.1381 1.167e-04 ***
## Joint Effect       21.7597 7.319e-05 ***
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     23.16       0.2303
## 2    30     37.55       0.1328
## 3    40     46.13       0.2013
## 4    50     48.05       0.5116
## 
## 
## Elapsed time : 0.3322079
egarch_31
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : eGARCH(3,1)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error  t value Pr(>|t|)
## omega  -0.920309    0.000220 -4179.20        0
## alpha1 -0.479239    0.000969  -494.35        0
## alpha2  0.633710    0.000272  2327.03        0
## alpha3  0.026277    0.000061   429.82        0
## beta1   0.800556    0.000114  7007.04        0
## gamma1 -0.664486    0.000485 -1369.00        0
## gamma2 -2.091609    0.000400 -5233.48        0
## gamma3  1.867172    0.000239  7808.64        0
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## omega  -0.920309    0.652466 -1.41051 0.158389
## alpha1 -0.479239    1.557952 -0.30761 0.758380
## alpha2  0.633710    0.063938  9.91136 0.000000
## alpha3  0.026277    0.074643  0.35203 0.724812
## beta1   0.800556    0.317097  2.52464 0.011582
## gamma1 -0.664486    0.262634 -2.53009 0.011403
## gamma2 -2.091609    1.570631 -1.33170 0.182959
## gamma3  1.867172    1.467327  1.27250 0.203196
## 
## LogLikelihood : 51.50231 
## 
## Information Criteria
## ------------------------------------
##                     
## Akaike       -1.4033
## Bayes        -1.1288
## Shibata      -1.4318
## Hannan-Quinn -1.2955
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      2.396  0.1217
## Lag[2*(p+q)+(p+q)-1][2]     2.473  0.1951
## Lag[4*(p+q)+(p+q)-1][5]     3.435  0.3334
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                          statistic p-value
## Lag[1]                     0.06826  0.7939
## Lag[2*(p+q)+(p+q)-1][11]   6.92242  0.3215
## Lag[4*(p+q)+(p+q)-1][19]  12.79442  0.2073
## d.o.f=4
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[5]     1.462 0.500 2.000  0.2266
## ARCH Lag[7]     1.856 1.473 1.746  0.5365
## ARCH Lag[9]     3.888 2.402 1.619  0.4143
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  5.6022
## Individual Statistics:              
## omega  0.02756
## alpha1 0.06821
## alpha2 0.06843
## alpha3 0.06838
## beta1  0.94555
## gamma1 0.06748
## gamma2 0.04101
## gamma3 0.02602
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.89 2.11 2.59
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value    prob sig
## Sign Bias          1.84033 0.07093   *
## Negative Sign Bias 0.06035 0.95209    
## Positive Sign Bias 0.54484 0.58799    
## Joint Effect       5.52819 0.13696    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     11.55       0.9040
## 2    30     22.19       0.8121
## 3    40     25.74       0.9493
## 4    50     43.21       0.7059
## 
## 
## Elapsed time : 1.138117

egarch_11 Akaike -1.1560 Bayes -1.0188 Shibata -1.1637 Hannan-Quinn -1.1022

egarch_12 Akaike -1.2221 Bayes -1.0505 Shibata -1.2338 Hannan-Quinn -1.1547

egarch_21 Akaike -1.08707 Bayes -0.88122 Shibata -1.10369 Hannan-Quinn -1.00625

egarch_13 Akaike -1.2526 Bayes -1.0467 Shibata -1.2692 Hannan-Quinn -1.1717

egarch_31 Akaike -1.4033 Bayes -1.1288 Shibata -1.4318 Hannan-Quinn -1.2955

Na podstawie kryteriów informacyjnych wybieramy model eGARCH(3,1).

Wykres funkcji autokorelacji na kwadratach zwrotów:

plot(egarch_31, which = 10)

Wypustki są nieistotne, ponieważ nie przekraczają obszaru krytycznego. Zatem nie występuje autokorelacja reszt.

Kolejnym krokiem jest obliczenie Value-At_Risk w in sample i out of sample oraz porównanie kwantyli empirycznych ze standardowym z rozkładu normalnego.

portfel_probka_in$zwrotystd3 <- (portfel_probka_in$zwroty - mean(portfel_probka_in$zwroty, na.rm=T)) /
  sd(portfel_probka_in$zwroty ,na.rm = T)
tail(portfel_probka_in)
##           Date      zwroty  zwrotystd        VaR zwrotystd2       VaR2
## 116 2019-04-26 -0.02431955 -0.2332355 -0.3440883 -0.2332355 -0.3723686
## 117 2019-04-27 -0.01574112 -0.1692550 -0.3447191 -0.1692550 -0.3745176
## 118 2019-04-28 -0.06799414 -0.5589739 -0.3453481 -0.5589739 -0.3766487
## 119 2019-04-29 -0.16912923 -1.3132701 -0.3459754 -1.3132701 -0.3787623
## 120 2019-04-30  0.29343999  2.1367114 -0.3466009  2.1367114 -0.3808582
## 121 2019-05-01 -0.06367050 -0.5267269 -0.3472246 -0.5267269 -0.3829369
##     zwrotystd3
## 116 -0.2332355
## 117 -0.1692550
## 118 -0.5589739
## 119 -1.3132701
## 120  2.1367114
## 121 -0.5267269
basicStats(portfel_probka_in$zwrotystd3)
##             X..portfel_probka_in.zwrotystd3
## nobs                              62.000000
## NAs                                0.000000
## Minimum                           -2.831300
## Maximum                            3.986365
## 1. Quartile                       -0.510065
## 3. Quartile                        0.520829
## Mean                               0.000000
## Median                            -0.059323
## Sum                                0.000000
## SE Mean                            0.127000
## LCL Mean                          -0.253952
## UCL Mean                           0.253952
## Variance                           1.000000
## Stdev                              1.000000
## Skewness                           0.632851
## Kurtosis                           3.294645
q01 <- quantile(portfel_probka_in$zwrotystd3, 0.01, na.rm = T)
q01
##        1% 
## -2.297398
qnorm(0.01, 0, 1)
## [1] -2.326348
str(egarch_11)
## Formal class 'uGARCHfit' [package "rugarch"] with 2 slots
##   ..@ fit  :List of 27
##   .. ..$ hessian        : num [1:4, 1:4] 210390819 52471493 -24386658 -56804749 52471493 ...
##   .. ..$ cvar           : num [1:4, 1:4] -1.92e-10 1.90e-08 -2.66e-10 -6.05e-10 1.90e-08 ...
##   .. ..$ var            : num [1:62] 0.0177 0.0198 0.0219 0.0246 0.0243 ...
##   .. ..$ sigma          : num [1:62] 0.133 0.141 0.148 0.157 0.156 ...
##   .. ..$ condH          : num NaN
##   .. ..$ z              : num [1:62] 0.351 -0.358 -0.326 -0.536 0.968 ...
##   .. ..$ LLH            : num 39.8
##   .. ..$ log.likelihoods: num [1:62] -1.036 -0.977 -0.939 -0.79 -0.472 ...
##   .. ..$ residuals      : num [1:62] 0.0467 -0.0504 -0.0482 -0.084 0.1508 ...
##   .. ..$ coef           : Named num [1:4] -0.2028 0.0111 0.99 -0.604
##   .. .. ..- attr(*, "names")= chr [1:4] "omega" "alpha1" "beta1" "gamma1"
##   .. ..$ robust.cvar    : num [1:4, 1:4] 1.00e-09 -1.32e-07 -1.98e-07 -4.39e-08 -1.32e-07 ...
##   .. ..$ A              : num [1:4, 1:4] 3393400 846314 -393333 -916206 846314 ...
##   .. ..$ B              : num [1:4, 1:4] 6389578 137865 -28177089 -2035575 137865 ...
##   .. ..$ scores         : num [1:62, 1:4] 0 0.436 0.938 1.172 0.151 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : NULL
##   .. .. .. ..$ : chr [1:4] "omega" "alpha1" "beta1" "gamma1"
##   .. ..$ se.coef        : num [1:4] 1.39e-05 1.55e-04 2.64e-04 2.45e-04
##   .. ..$ tval           : Named num [1:4] -14626.7 71.5 3743.4 -2463
##   .. .. ..- attr(*, "names")= chr [1:4] "omega" "alpha1" "beta1" "gamma1"
##   .. ..$ matcoef        : num [1:4, 1:4] -2.03e-01 1.11e-02 9.90e-01 -6.04e-01 1.39e-05 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:4] "omega" "alpha1" "beta1" "gamma1"
##   .. .. .. ..$ : chr [1:4] " Estimate" " Std. Error" " t value" "Pr(>|t|)"
##   .. ..$ robust.se.coef : num [1:4] 3.17e-05 4.16e-03 6.25e-03 1.39e-03
##   .. ..$ robust.tval    : Named num [1:4] -6401.99 2.66 158.37 -435.4
##   .. .. ..- attr(*, "names")= chr [1:4] "omega" "alpha1" "beta1" "gamma1"
##   .. ..$ robust.matcoef : num [1:4, 1:4] -2.03e-01 1.11e-02 9.90e-01 -6.04e-01 3.17e-05 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:4] "omega" "alpha1" "beta1" "gamma1"
##   .. .. .. ..$ : chr [1:4] " Estimate" " Std. Error" " t value" "Pr(>|t|)"
##   .. ..$ fitted.values  : num [1:62] 0 0 0 0 0 0 0 0 0 0 ...
##   .. ..$ convergence    : num 0
##   .. ..$ kappa          : num 0.798
##   .. ..$ persistence    : num 0.99
##   .. ..$ timer          : 'difftime' num 0.200273990631104
##   .. .. ..- attr(*, "units")= chr "secs"
##   .. ..$ ipars          : num [1:19, 1:6] 0 0 0 0 0 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:19] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:6] "Level" "Fixed" "Include" "Estimate" ...
##   .. ..$ solver         :List of 2
##   .. .. ..$ sol :List of 10
##   .. .. .. ..$ pars       : Named num [1:4] -0.2028 0.0111 0.99 -0.604
##   .. .. .. .. ..- attr(*, "names")= chr [1:4] "omega" "alpha1" "beta1" "gamma1"
##   .. .. .. ..$ convergence: num 0
##   .. .. .. ..$ values     : num [1:3] 26.8 -39.8 -39.8
##   .. .. .. ..$ lagrange   : num [1, 1] -19.3
##   .. .. .. ..$ hessian    : num [1:5, 1:5] 0.409 -9.948 -0.351 38.442 3.127 ...
##   .. .. .. ..$ ineqx0     : Named num 1.01
##   .. .. .. .. ..- attr(*, "names")= chr ""
##   .. .. .. ..$ nfuneval   : num 346
##   .. .. .. ..$ outer.iter : num 2
##   .. .. .. ..$ elapsed    : 'difftime' num 0.197808027267456
##   .. .. .. .. ..- attr(*, "units")= chr "secs"
##   .. .. .. ..$ vscale     : num [1:6] 1 1 1 1 1 1
##   .. .. ..$ hess: NULL
##   ..@ model:List of 11
##   .. ..$ modelinc  : Named num [1:22] 0 0 0 0 0 0 1 1 1 1 ...
##   .. .. ..- attr(*, "names")= chr [1:22] "mu" "ar" "ma" "arfima" ...
##   .. ..$ modeldesc :List of 3
##   .. .. ..$ distribution: chr "norm"
##   .. .. ..$ distno      : int 1
##   .. .. ..$ vmodel      : chr "eGARCH"
##   .. ..$ modeldata :List of 4
##   .. .. ..$ T     : int 62
##   .. .. ..$ data  : num [1:62] 0.0467 -0.0504 -0.0482 -0.084 0.1508 ...
##   .. .. ..$ index : POSIXct[1:62], format: "1970-01-02 01:00:00" ...
##   .. .. ..$ period: 'difftime' num 1
##   .. .. .. ..- attr(*, "units")= chr "days"
##   .. ..$ pars      : num [1:19, 1:6] 0 0 0 0 0 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:19] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:6] "Level" "Fixed" "Include" "Estimate" ...
##   .. ..$ start.pars: NULL
##   .. ..$ fixed.pars: NULL
##   .. ..$ maxOrder  : num 1
##   .. ..$ pos.matrix: num [1:21, 1:3] 0 0 0 0 0 0 1 2 3 4 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:21] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:3] "start" "stop" "include"
##   .. ..$ fmodel    : NULL
##   .. ..$ pidx      : num [1:19, 1:2] 1 2 3 4 5 6 7 8 9 10 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:19] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:2] "begin" "end"
##   .. ..$ n.start   : num 0
egarch_11@fit$sigma
##  [1] 0.13317468 0.14081044 0.14789054 0.15678545 0.15573713 0.13691303
##  [7] 0.13861630 0.14665302 0.15324952 0.14333067 0.15222139 0.13280632
## [13] 0.10981052 0.11778829 0.10383558 0.11826613 0.12657977 0.14043658
## [19] 0.14995027 0.16865121 0.18299938 0.16890772 0.18111240 0.17749405
## [25] 0.17861736 0.17233731 0.20118951 0.17972864 0.20998713 0.22709194
## [31] 0.25878785 0.27004206 0.27907832 0.18284635 0.17658659 0.20596295
## [37] 0.17623342 0.19014929 0.20032128 0.18882282 0.20804209 0.23431662
## [43] 0.16760901 0.15942699 0.16322303 0.16208049 0.13817030 0.13729482
## [49] 0.15033963 0.15182063 0.17589596 0.19677701 0.18584143 0.18770494
## [55] 0.21241861 0.17139729 0.13263305 0.14708081 0.16680423 0.17225059
## [61] 0.14903015 0.09741781
portfel_probka_in$VaR3 <- q01 * egarch_11@fit$sigma
tail(portfel_probka_in)
##           Date      zwroty  zwrotystd        VaR zwrotystd2       VaR2
## 116 2019-04-26 -0.02431955 -0.2332355 -0.3440883 -0.2332355 -0.3723686
## 117 2019-04-27 -0.01574112 -0.1692550 -0.3447191 -0.1692550 -0.3745176
## 118 2019-04-28 -0.06799414 -0.5589739 -0.3453481 -0.5589739 -0.3766487
## 119 2019-04-29 -0.16912923 -1.3132701 -0.3459754 -1.3132701 -0.3787623
## 120 2019-04-30  0.29343999  2.1367114 -0.3466009  2.1367114 -0.3808582
## 121 2019-05-01 -0.06367050 -0.5267269 -0.3472246 -0.5267269 -0.3829369
##     zwrotystd3       VaR3
## 116 -0.2332355 -0.3047109
## 117 -0.1692550 -0.3379031
## 118 -0.5589739 -0.3832157
## 119 -1.3132701 -0.3957281
## 120  2.1367114 -0.3423815
## 121 -0.5267269 -0.2238074
# wykres zwrotów i var
plot(portfel_probka_in$Date, portfel_probka_in$zwroty, col = "red", lwd = 1, type = 'l',
     ylim = c(-2,2))
abline(h = 0, lty = 2)
lines(portfel_probka_in$Date, portfel_probka_in$VaR3, type = 'l', col = "green")

a =sum(portfel_probka_in$zwroty < portfel_probka_in$VaR3) / length(portfel_probka_in$VaR3)
a
## [1] 0

W 0% przypadków straty przekroczyły zakładany poziom VaR.

VaR dla out of sample:

sigma.forecast3 <- ugarchforecast(egarch_11, n.ahead = 1)

str(sigma.forecast3)
## Formal class 'uGARCHforecast' [package "rugarch"] with 2 slots
##   ..@ forecast:List of 6
##   .. ..$ n.ahead  : num 1
##   .. ..$ N        : num 62
##   .. ..$ n.start  : num 0
##   .. ..$ n.roll   : num 0
##   .. ..$ sigmaFor : num [1, 1] 0.0938
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr "T+1"
##   .. .. .. ..$ : chr "1970-03-04 01:00:00"
##   .. ..$ seriesFor: num [1, 1] 0
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr "T+1"
##   .. .. .. ..$ : chr "1970-03-04 01:00:00"
##   ..@ model   :List of 11
##   .. ..$ modelinc  : Named num [1:22] 0 0 0 0 0 0 1 1 1 1 ...
##   .. .. ..- attr(*, "names")= chr [1:22] "mu" "ar" "ma" "arfima" ...
##   .. ..$ modeldesc :List of 3
##   .. .. ..$ distribution: chr "norm"
##   .. .. ..$ distno      : int 1
##   .. .. ..$ vmodel      : chr "eGARCH"
##   .. ..$ modeldata :List of 6
##   .. .. ..$ T        : int 62
##   .. .. ..$ data     : num [1:62] 0.0467 -0.0504 -0.0482 -0.084 0.1508 ...
##   .. .. ..$ index    : POSIXct[1:62], format: "1970-01-02 01:00:00" ...
##   .. .. ..$ period   : 'difftime' num 1
##   .. .. .. ..- attr(*, "units")= chr "days"
##   .. .. ..$ sigma    : num [1:62] 0.133 0.141 0.148 0.157 0.156 ...
##   .. .. ..$ residuals: num [1:62] 0.0467 -0.0504 -0.0482 -0.084 0.1508 ...
##   .. ..$ pars      : num [1:19, 1:6] 0 0 0 0 0 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:19] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:6] "Level" "Fixed" "Include" "Estimate" ...
##   .. ..$ start.pars: NULL
##   .. ..$ fixed.pars: NULL
##   .. ..$ maxOrder  : num 1
##   .. ..$ pos.matrix: num [1:21, 1:3] 0 0 0 0 0 0 1 2 3 4 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:21] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:3] "start" "stop" "include"
##   .. ..$ fmodel    : NULL
##   .. ..$ pidx      : num [1:19, 1:2] 1 2 3 4 5 6 7 8 9 10 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:19] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:2] "begin" "end"
##   .. ..$ n.start   : num 0
str(sigma.forecast3@forecast)
## List of 6
##  $ n.ahead  : num 1
##  $ N        : num 62
##  $ n.start  : num 0
##  $ n.roll   : num 0
##  $ sigmaFor : num [1, 1] 0.0938
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr "T+1"
##   .. ..$ : chr "1970-03-04 01:00:00"
##  $ seriesFor: num [1, 1] 0
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr "T+1"
##   .. ..$ : chr "1970-03-04 01:00:00"
sigma.forecast3@forecast$sigmaFor
##     1970-03-04 01:00:00
## T+1          0.09377283
sigma.forecast33 <- sigma.forecast3@forecast$sigmaFor[1, 1]

# 1-dniowy VaR:
q01 * sigma.forecast33
##         1% 
## -0.2154335
portfel$obs<-1:length(portfel$zwroty)
start  <- portfel$obs[portfel$Date == as.Date("2019-03-02")]
finish <- portfel$obs[portfel$Date == as.Date("2019-06-01")]
portfel3 <-portfel[start:finish, ]
VaR3 <- rep(NA, times = finish - start + 1)

time1 <- Sys.time()
for (k in start:finish) {
  tmp.data <- portfel[portfel$obs <= (k - 1), ]
  tmp.data <- tmp.data[as.Date("2019-01-02") <= tmp.data$Date, ]
  tmp.data$zwrotystd <- (tmp.data$zwroty - mean(tmp.data$zwroty, na.rm = T)) /
    sd(tmp.data$zwroty, na.rm = T)
  q01 <- quantile(tmp.data$zwrotystd, 0.01, na.rm = T)
  espec11 = ugarchspec(variance.model = list(model ="eGARCH",
                                             garchOrder = c(1, 1)),
                       mean.model = list(armaOrder = c(0, 0), include.mean = F),
                       distribution.model = "norm")
  tmp.egarch_13 <- ugarchfit(spec = espec_13, data = na.omit(portfel$zwroty))
  sigma.forecast3  <- ugarchforecast(tmp.egarch_13, n.ahead = 1)
  sigma.forecast33 <- sigma.forecast3@forecast$sigmaFor[1, 1]
  VaR3[k - start + 1] <- q01 * sigma.forecast33
}
time2 <- Sys.time()

time2 - time1
## Time difference of 20.01943 secs
portfel3$VaR3 <- VaR3

head(portfel3)
##          Date      zwroty obs       VaR3
## 61 2019-03-02 -0.05042352  61 -0.3896493
## 62 2019-03-03 -0.04822774  62 -0.3906934
## 63 2019-03-04 -0.08402545  63 -0.3917885
## 64 2019-03-05  0.15076191  64 -0.3915307
## 65 2019-03-06 -0.06548814  65 -0.3921967
## 66 2019-03-07  0.04815102  66 -0.3925820
tail(portfel3)
##           Date      zwroty obs       VaR3
## 147 2019-05-27  0.19794321 147 -0.3396306
## 148 2019-05-28 -0.00585839 148 -0.3400340
## 149 2019-05-29  0.03274560 149 -0.3410705
## 150 2019-05-30 -0.26059968 150 -0.3423246
## 151 2019-05-31  0.18577773 151 -0.3379694
## 152 2019-06-01  0.09272815 152 -0.3385231
plot(portfel3$Date, portfel3$zwroty, col = "red", lwd = 1, type = 'l',
     ylim = c(-1, 1))
abline(h = 0, lty = 2)
lines(portfel3$Date, portfel3$VaR3, type = 'l', col = "green")

b = sum(portfel3$zwroty < portfel3$VaR3) / length(portfel3$VaR3)
b
## [1] 0

W 0% przypadków straty przekroczyły zakładany poziom VaR.

Porównanie oszacowań wybranych modeli:

garch_11 
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : sGARCH(1,1)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.007806    0.017410 0.448338  0.65391
## omega   0.000105    0.001974 0.053073  0.95767
## alpha1  0.000000    0.048064 0.000000  1.00000
## beta1   0.999000    0.127840 7.814467  0.00000
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.007806    0.011901 0.655892 0.511894
## omega   0.000105    0.003070 0.034119 0.972782
## alpha1  0.000000    0.114887 0.000000 1.000000
## beta1   0.999000    0.242399 4.121296 0.000038
## 
## LogLikelihood : 37.53098 
## 
## Information Criteria
## ------------------------------------
##                      
## Akaike       -1.08164
## Bayes        -0.94441
## Shibata      -1.08932
## Hannan-Quinn -1.02776
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      1.332  0.2484
## Lag[2*(p+q)+(p+q)-1][2]     1.379  0.3901
## Lag[4*(p+q)+(p+q)-1][5]     2.567  0.4917
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                    0.01737  0.8951
## Lag[2*(p+q)+(p+q)-1][5]   0.75792  0.9116
## Lag[4*(p+q)+(p+q)-1][9]   2.60867  0.8214
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3]   0.07056 0.500 2.000  0.7905
## ARCH Lag[5]   0.45740 1.440 1.667  0.8961
## ARCH Lag[7]   1.19966 2.315 1.543  0.8791
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  2.6032
## Individual Statistics:              
## mu     0.16164
## omega  0.05206
## alpha1 0.05538
## beta1  0.04945
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.07 1.24 1.6
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias           0.2212 0.8257    
## Negative Sign Bias  0.1040 0.9175    
## Positive Sign Bias  0.1687 0.8666    
## Joint Effect        0.0537 0.9967    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     14.77       0.7368
## 2    30     26.06       0.6220
## 3    40     27.03       0.9262
## 4    50     46.06       0.5929
## 
## 
## Elapsed time : 0.10078
k.tgarch_11 
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : fGARCH(1,1)
## fGARCH Sub-Model : TGARCH
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error   t value Pr(>|t|)
## omega   0.002279    0.004699  0.485102   0.6276
## alpha1  0.000000    0.000002  0.000131   0.9999
## beta1   0.991708    0.038038 26.071406   0.0000
## eta11  -0.985951    1.677084 -0.587896   0.5566
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## omega   0.002279    0.004043  0.56384  0.57286
## alpha1  0.000000    0.000000  0.30145  0.76307
## beta1   0.991708    0.029365 33.77200  0.00000
## eta11  -0.985951    4.316444 -0.22842  0.81932
## 
## LogLikelihood : 39.61836 
## 
## Information Criteria
## ------------------------------------
##                     
## Akaike       -1.1490
## Bayes        -1.0117
## Shibata      -1.1567
## Hannan-Quinn -1.0951
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      1.806  0.1790
## Lag[2*(p+q)+(p+q)-1][2]     1.865  0.2862
## Lag[4*(p+q)+(p+q)-1][5]     2.986  0.4097
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                    0.06796  0.7943
## Lag[2*(p+q)+(p+q)-1][5]   0.86983  0.8884
## Lag[4*(p+q)+(p+q)-1][9]   2.68274  0.8100
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3]   0.08566 0.500 2.000  0.7698
## ARCH Lag[5]   0.70240 1.440 1.667  0.8227
## ARCH Lag[7]   1.50849 2.315 1.543  0.8197
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  0.2742
## Individual Statistics:              
## omega  0.05682
## alpha1 0.05023
## beta1  0.05659
## eta11  0.06104
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.07 1.24 1.6
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias           0.4090 0.6841    
## Negative Sign Bias  0.1023 0.9189    
## Positive Sign Bias  0.3188 0.7511    
## Joint Effect        0.1904 0.9791    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     19.29       0.4384
## 2    30     23.16       0.7691
## 3    40     33.48       0.7191
## 4    50     34.77       0.9378
## 
## 
## Elapsed time : 0.1547849
egarch_31
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : eGARCH(3,1)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error  t value Pr(>|t|)
## omega  -0.920309    0.000220 -4179.20        0
## alpha1 -0.479239    0.000969  -494.35        0
## alpha2  0.633710    0.000272  2327.03        0
## alpha3  0.026277    0.000061   429.82        0
## beta1   0.800556    0.000114  7007.04        0
## gamma1 -0.664486    0.000485 -1369.00        0
## gamma2 -2.091609    0.000400 -5233.48        0
## gamma3  1.867172    0.000239  7808.64        0
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## omega  -0.920309    0.652466 -1.41051 0.158389
## alpha1 -0.479239    1.557952 -0.30761 0.758380
## alpha2  0.633710    0.063938  9.91136 0.000000
## alpha3  0.026277    0.074643  0.35203 0.724812
## beta1   0.800556    0.317097  2.52464 0.011582
## gamma1 -0.664486    0.262634 -2.53009 0.011403
## gamma2 -2.091609    1.570631 -1.33170 0.182959
## gamma3  1.867172    1.467327  1.27250 0.203196
## 
## LogLikelihood : 51.50231 
## 
## Information Criteria
## ------------------------------------
##                     
## Akaike       -1.4033
## Bayes        -1.1288
## Shibata      -1.4318
## Hannan-Quinn -1.2955
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      2.396  0.1217
## Lag[2*(p+q)+(p+q)-1][2]     2.473  0.1951
## Lag[4*(p+q)+(p+q)-1][5]     3.435  0.3334
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                          statistic p-value
## Lag[1]                     0.06826  0.7939
## Lag[2*(p+q)+(p+q)-1][11]   6.92242  0.3215
## Lag[4*(p+q)+(p+q)-1][19]  12.79442  0.2073
## d.o.f=4
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[5]     1.462 0.500 2.000  0.2266
## ARCH Lag[7]     1.856 1.473 1.746  0.5365
## ARCH Lag[9]     3.888 2.402 1.619  0.4143
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  5.6022
## Individual Statistics:              
## omega  0.02756
## alpha1 0.06821
## alpha2 0.06843
## alpha3 0.06838
## beta1  0.94555
## gamma1 0.06748
## gamma2 0.04101
## gamma3 0.02602
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.89 2.11 2.59
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value    prob sig
## Sign Bias          1.84033 0.07093   *
## Negative Sign Bias 0.06035 0.95209    
## Positive Sign Bias 0.54484 0.58799    
## Joint Effect       5.52819 0.13696    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     11.55       0.9040
## 2    30     22.19       0.8121
## 3    40     25.74       0.9493
## 4    50     43.21       0.7059
## 
## 
## Elapsed time : 1.138117

Spośród wybranych modeli najmniejszą wartość kryteriów informacyjnych ma eGARCH(3,1).

#Prognozowanie wariancji bezwarunkowej dla eGARCH(3,1)

egarch_31 <- garchFit(formula = ~ garch(3, 1),
                      data = na.omit(portfel$zwroty),
                      include.mean = F,
                      cond.dist = "norm",
                      trace = F)
egarch_31@fit$par
##       omega      alpha1      alpha2      alpha3       beta1 
## 0.006961535 0.031678286 0.000000010 0.126104199 0.574852985
var_uncond <- egarch_31@fit$par[1] / (1 - egarch_31@fit$par[2]
                                        - egarch_31@fit$par[3])

names(var_uncond) <- "unconditional variance egarch"
var_uncond
## unconditional variance egarch 
##                   0.007189279
fore31 <- predict(egarch_31, n.ahead = 20)
head(fore31)
##   meanForecast meanError standardDeviation
## 1            0 0.1404761         0.1404761
## 2            0 0.1376984         0.1376984
## 3            0 0.1381412         0.1381412
## 4            0 0.1449981         0.1449981
## 5            0 0.1486760         0.1486760
## 6            0 0.1509142         0.1509142
plot(fore31[, 3] ^ 2, type = "l")
abline(h = var_uncond, col = "red", lty = 2)
title(main = "Warunkowa i bezwarunkowa wariancja zwrotów eGARCH(3,1)")

# Wykres wariancji out of sample GARCH(1,1)

spec_11 <- ugarchspec(variance.model = list(model = "sGARCH",
                                           garchOrder = c(1, 1)),
                     mean.model = list(armaOrder = c(0, 0),
                                       include.mean = T),
                     distribution.model = "norm")

garch_11 <- ugarchfit(spec = spec_11,
                     data = na.omit(portfel$zwroty))
garch_11
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : sGARCH(1,1)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error  t value Pr(>|t|)
## mu     -0.005311    0.009586 -0.55405 0.579541
## omega   0.004206    0.002560  1.64307 0.100369
## alpha1  0.097716    0.055060  1.77473 0.075943
## beta1   0.741153    0.131971  5.61603 0.000000
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## mu     -0.005311    0.008305 -0.63952 0.522487
## omega   0.004206    0.001982  2.12263 0.033785
## alpha1  0.097716    0.055143  1.77205 0.076387
## beta1   0.741153    0.090549  8.18515 0.000000
## 
## LogLikelihood : 104.2212 
## 
## Information Criteria
## ------------------------------------
##                      
## Akaike       -0.81481
## Bayes        -0.75781
## Shibata      -0.81532
## Hannan-Quinn -0.79186
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      1.972  0.1603
## Lag[2*(p+q)+(p+q)-1][2]     2.622  0.1777
## Lag[4*(p+q)+(p+q)-1][5]     3.133  0.3833
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                    0.03868  0.8441
## Lag[2*(p+q)+(p+q)-1][5]   2.12420  0.5894
## Lag[4*(p+q)+(p+q)-1][9]   3.16173  0.7320
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3]  0.000784 0.500 2.000  0.9777
## ARCH Lag[5]  2.308383 1.440 1.667  0.4070
## ARCH Lag[7]  2.610566 2.315 1.543  0.5899
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  0.5333
## Individual Statistics:             
## mu     0.2432
## omega  0.1595
## alpha1 0.1900
## beta1  0.1834
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.07 1.24 1.6
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value   prob sig
## Sign Bias           1.5045 0.1338    
## Negative Sign Bias  1.1499 0.2513    
## Positive Sign Bias  0.7339 0.4637    
## Joint Effect        2.4580 0.4829    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     32.86      0.02494
## 2    30     41.32      0.06460
## 3    40     51.56      0.08584
## 4    50     49.12      0.46824
## 
## 
## Elapsed time : 0.0890789
plot(ugarchforecast(garch_11, n.ahead=20), which = 3)

# Wykres wariancji out of sample eGARCH(3,1)

espec_31 = ugarchspec(variance.model = list(model ="eGARCH",
                                           garchOrder = c(3, 1)),
                     mean.model = list(armaOrder = c(0, 0), include.mean = F),
                     distribution.model = "norm")

egarch_31 <- ugarchfit(spec = espec_31, data = na.omit(portfel$zwroty))
egarch_31
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : eGARCH(3,1)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error  t value Pr(>|t|)
## omega  -0.855075    0.486417 -1.75790 0.078764
## alpha1 -0.028408    0.089092 -0.31886 0.749830
## alpha2  0.043584    0.115669  0.37680 0.706323
## alpha3  0.039053    0.117465  0.33247 0.739536
## beta1   0.762994    0.132865  5.74264 0.000000
## gamma1  0.168874    0.153801  1.09800 0.272205
## gamma2 -0.105016    0.162740 -0.64530 0.518734
## gamma3  0.258664    0.189544  1.36467 0.172358
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## omega  -0.855075    0.340554 -2.51084 0.012045
## alpha1 -0.028408    0.092998 -0.30547 0.760008
## alpha2  0.043584    0.117257  0.37169 0.710120
## alpha3  0.039053    0.125862  0.31029 0.756343
## beta1   0.762994    0.094801  8.04834 0.000000
## gamma1  0.168874    0.151188  1.11698 0.264003
## gamma2 -0.105016    0.147124 -0.71379 0.475356
## gamma3  0.258664    0.197735  1.30813 0.190829
## 
## LogLikelihood : 106.1957 
## 
## Information Criteria
## ------------------------------------
##                      
## Akaike       -0.79834
## Bayes        -0.68434
## Shibata      -0.80037
## Hannan-Quinn -0.75244
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      2.626  0.1051
## Lag[2*(p+q)+(p+q)-1][2]     3.251  0.1200
## Lag[4*(p+q)+(p+q)-1][5]     3.757  0.2860
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                          statistic p-value
## Lag[1]                     0.05428  0.8158
## Lag[2*(p+q)+(p+q)-1][11]   1.60387  0.9809
## Lag[4*(p+q)+(p+q)-1][19]   2.79822  0.9966
## d.o.f=4
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[5]    0.1230 0.500 2.000  0.7258
## ARCH Lag[7]    0.4655 1.473 1.746  0.9064
## ARCH Lag[9]    0.5633 2.402 1.619  0.9792
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  1.153
## Individual Statistics:              
## omega  0.11240
## alpha1 0.17869
## alpha2 0.07197
## alpha3 0.08578
## beta1  0.10303
## gamma1 0.08577
## gamma2 0.11828
## gamma3 0.11169
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.89 2.11 2.59
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value    prob sig
## Sign Bias           2.0622 0.04026  **
## Negative Sign Bias  1.2585 0.20943    
## Positive Sign Bias  0.9056 0.36604    
## Joint Effect        4.3377 0.22723    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     28.31      0.07766
## 2    30     41.32      0.06460
## 3    40     42.46      0.32445
## 4    50     61.32      0.11140
## 
## 
## Elapsed time : 0.1796088
str(egarch_31)
## Formal class 'uGARCHfit' [package "rugarch"] with 2 slots
##   ..@ fit  :List of 27
##   .. ..$ hessian        : num [1:8, 1:8] 982.53 -9.22 -28.89 -25.33 -3650.39 ...
##   .. ..$ cvar           : num [1:8, 1:8] 0.2366 0.00608 -0.00163 0.00666 0.06447 ...
##   .. ..$ var            : num [1:246] 0.0264 0.0264 0.0264 0.0221 0.0217 ...
##   .. ..$ sigma          : num [1:246] 0.162 0.162 0.162 0.149 0.147 ...
##   .. ..$ condH          : num 3.59
##   .. ..$ z              : num [1:246] 0.409 -0.735 0.428 0.413 1.674 ...
##   .. ..$ LLH            : num 106
##   .. ..$ log.likelihoods: num [1:246] -0.815 -0.629 -0.807 -0.902 0.406 ...
##   .. ..$ residuals      : num [1:246] 0.0664 -0.1193 0.0695 0.0614 0.2468 ...
##   .. ..$ coef           : Named num [1:8] -0.8551 -0.0284 0.0436 0.0391 0.763 ...
##   .. .. ..- attr(*, "names")= chr [1:8] "omega" "alpha1" "alpha2" "alpha3" ...
##   .. ..$ robust.cvar    : num [1:8, 1:8] 0.115977 0.008269 -0.000561 0.005163 0.031999 ...
##   .. ..$ A              : num [1:8, 1:8] 3.994 -0.0375 -0.1174 -0.103 -14.839 ...
##   .. ..$ B              : num [1:8, 1:8] 7.582 -0.202 -0.473 -0.24 -28.331 ...
##   .. ..$ scores         : num [1:246, 1:8] 0 0 0 0.415 -1.562 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : NULL
##   .. .. .. ..$ : chr [1:8] "omega" "alpha1" "alpha2" "alpha3" ...
##   .. ..$ se.coef        : num [1:8] 0.4864 0.0891 0.1157 0.1175 0.1329 ...
##   .. ..$ tval           : Named num [1:8] -1.758 -0.319 0.377 0.332 5.743 ...
##   .. .. ..- attr(*, "names")= chr [1:8] "omega" "alpha1" "alpha2" "alpha3" ...
##   .. ..$ matcoef        : num [1:8, 1:4] -0.8551 -0.0284 0.0436 0.0391 0.763 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:8] "omega" "alpha1" "alpha2" "alpha3" ...
##   .. .. .. ..$ : chr [1:4] " Estimate" " Std. Error" " t value" "Pr(>|t|)"
##   .. ..$ robust.se.coef : num [1:8] 0.3406 0.093 0.1173 0.1259 0.0948 ...
##   .. ..$ robust.tval    : Named num [1:8] -2.511 -0.305 0.372 0.31 8.048 ...
##   .. .. ..- attr(*, "names")= chr [1:8] "omega" "alpha1" "alpha2" "alpha3" ...
##   .. ..$ robust.matcoef : num [1:8, 1:4] -0.8551 -0.0284 0.0436 0.0391 0.763 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:8] "omega" "alpha1" "alpha2" "alpha3" ...
##   .. .. .. ..$ : chr [1:4] " Estimate" " Std. Error" " t value" "Pr(>|t|)"
##   .. ..$ fitted.values  : num [1:246] 0 0 0 0 0 0 0 0 0 0 ...
##   .. ..$ convergence    : num 0
##   .. ..$ kappa          : num 0.798
##   .. ..$ persistence    : num 0.763
##   .. ..$ timer          : 'difftime' num 0.179608821868896
##   .. .. ..- attr(*, "units")= chr "secs"
##   .. ..$ ipars          : num [1:23, 1:6] 0 0 0 0 0 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:23] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:6] "Level" "Fixed" "Include" "Estimate" ...
##   .. ..$ solver         :List of 2
##   .. .. ..$ sol :List of 10
##   .. .. .. ..$ pars       : Named num [1:8] -0.8551 -0.0284 0.0436 0.0391 0.763 ...
##   .. .. .. .. ..- attr(*, "names")= chr [1:8] "omega" "alpha1" "alpha2" "alpha3" ...
##   .. .. .. ..$ convergence: num 0
##   .. .. .. ..$ values     : num [1:4] 168 -106 -106 -106
##   .. .. .. ..$ lagrange   : num [1, 1] 4.2e-07
##   .. .. .. ..$ hessian    : num [1:9, 1:9] 0.3988 -0.0967 -0.1442 -0.5164 -0.1248 ...
##   .. .. .. ..$ ineqx0     : Named num 1.31
##   .. .. .. .. ..- attr(*, "names")= chr ""
##   .. .. .. ..$ nfuneval   : num 296
##   .. .. .. ..$ outer.iter : num 3
##   .. .. .. ..$ elapsed    : 'difftime' num 0.177961111068726
##   .. .. .. .. ..- attr(*, "units")= chr "secs"
##   .. .. .. ..$ vscale     : num [1:10] 1 1 1 1 1 1 1 1 1 1
##   .. .. ..$ hess: NULL
##   ..@ model:List of 11
##   .. ..$ modelinc  : Named num [1:22] 0 0 0 0 0 0 1 3 1 3 ...
##   .. .. ..- attr(*, "names")= chr [1:22] "mu" "ar" "ma" "arfima" ...
##   .. ..$ modeldesc :List of 3
##   .. .. ..$ distribution: chr "norm"
##   .. .. ..$ distno      : int 1
##   .. .. ..$ vmodel      : chr "eGARCH"
##   .. ..$ modeldata :List of 4
##   .. .. ..$ T     : int 246
##   .. .. ..$ data  : num [1:246] 0.0664 -0.1193 0.0695 0.0614 0.2468 ...
##   .. .. ..$ index : POSIXct[1:246], format: "1970-01-02 01:00:00" ...
##   .. .. ..$ period: 'difftime' num 1
##   .. .. .. ..- attr(*, "units")= chr "days"
##   .. ..$ pars      : num [1:23, 1:6] 0 0 0 0 0 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:23] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:6] "Level" "Fixed" "Include" "Estimate" ...
##   .. ..$ start.pars: NULL
##   .. ..$ fixed.pars: NULL
##   .. ..$ maxOrder  : num 3
##   .. ..$ pos.matrix: num [1:21, 1:3] 0 0 0 0 0 0 1 2 5 6 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:21] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:3] "start" "stop" "include"
##   .. ..$ fmodel    : NULL
##   .. ..$ pidx      : num [1:19, 1:2] 1 2 3 4 5 6 7 8 11 12 ...
##   .. .. ..- attr(*, "dimnames")=List of 2
##   .. .. .. ..$ : chr [1:19] "mu" "ar" "ma" "arfima" ...
##   .. .. .. ..$ : chr [1:2] "begin" "end"
##   .. ..$ n.start   : num 0
plot(ugarchforecast(egarch_31, n.ahead=20), which = 3)

# Wykres wariancji out of sample tGARCH(1,1)

tspec_11 <- ugarchspec(variance.model = list(model = "fGARCH",
                                            garchOrder = c(1, 1),
                                            submodel = "TGARCH"),
                      mean.model = list(armaOrder = c(0, 0),
                                        include.mean = F),
                      distribution.model = "norm")

k.tgarch_11 <- ugarchfit(spec = tspec_11, data = na.omit(portfel$zwroty))
k.tgarch_11
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : fGARCH(1,1)
## fGARCH Sub-Model : TGARCH
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error  t value Pr(>|t|)
## omega   0.000857    0.001332  0.64349 0.519906
## alpha1  0.038490    0.009125  4.21834 0.000025
## beta1   0.967384    0.022337 43.30859 0.000000
## eta11  -1.000000    0.464118 -2.15462 0.031191
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## omega   0.000857    0.003340  0.25661 0.797478
## alpha1  0.038490    0.020511  1.87655 0.060579
## beta1   0.967384    0.028911 33.46113 0.000000
## eta11  -1.000000    0.664556 -1.50476 0.132385
## 
## LogLikelihood : 103.3991 
## 
## Information Criteria
## ------------------------------------
##                      
## Akaike       -0.80812
## Bayes        -0.75113
## Shibata      -0.80864
## Hannan-Quinn -0.78517
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      3.601 0.05776
## Lag[2*(p+q)+(p+q)-1][2]     4.123 0.06988
## Lag[4*(p+q)+(p+q)-1][5]     4.858 0.16482
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                     0.2414  0.6232
## Lag[2*(p+q)+(p+q)-1][5]    2.5203  0.5016
## Lag[4*(p+q)+(p+q)-1][9]    4.3413  0.5320
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3]    0.1146 0.500 2.000  0.7350
## ARCH Lag[5]    4.4293 1.440 1.667  0.1390
## ARCH Lag[7]    5.1382 2.315 1.543  0.2109
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  0.3099
## Individual Statistics:              
## omega  0.08987
## alpha1 0.09071
## beta1  0.09905
## eta11  0.06369
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.07 1.24 1.6
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value    prob sig
## Sign Bias           1.8285 0.06871   *
## Negative Sign Bias  1.8698 0.06272   *
## Positive Sign Bias  0.5644 0.57304    
## Joint Effect        4.5618 0.20684    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic p-value(g-1)
## 1    20     28.15      0.08065
## 2    30     36.44      0.16112
## 3    40     49.28      0.12517
## 4    50     63.76      0.07656
## 
## 
## Elapsed time : 0.1639779
plot(ugarchforecast(k.tgarch_11, n.ahead=20), which = 3)

Najlepszym z powyższych modeli jest model eGARCH(3,1). Wszystkie oszacowane parametry modelu są istotne.